【问题标题】:iOS Swift, How to differentiate the steps using Apple Watch and iPhone?iOS Swift,如何区分使用 Apple Watch 和 iPhone 的步骤?
【发布时间】:2018-09-17 17:22:15
【问题描述】:

我正在使用 HealthKit 框架来检索使用 HKSource 的用户的步骤。 在 Xcode Objective C 中,我使用 Bundle Identifier 来区分 watch/iPhone 的步骤。但是使用 Swift 我无法做到这一点。请建议。

提前致谢。

【问题讨论】:

    标签: ios swift apple-watch healthkit hksamplequery


    【解决方案1】:

    在查询选项中使用.separateBySource。例如:

    guard let sampleType = HKObjectType.quantityType(forIdentifier: .stepCount)
        else {
            fatalError("Couldn't create the quantityType for .stepCount")
    }
    let calendar = Calendar.current
    var dateComponents = DateComponents()
    dateComponents.day = 1
    
    var anchorComponents = calendar.dateComponents([.day, .month, .year], from: Date())
    anchorComponents.hour = 0
    guard let anchorDate = calendar.date(from: anchorComponents) else {
        fatalError("Couldn't get the anchor date")
    }
    
    let stepsCumulativeQuery =
        HKStatisticsCollectionQuery(quantityType: sampleType,
                                    quantitySamplePredicate: nil,
                                    options: [.cumulativeSum , .separateBySource],
                                    anchorDate: anchorDate,
                                    intervalComponents: dateComponents)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      相关资源
      最近更新 更多