【发布时间】:2018-09-17 17:22:15
【问题描述】:
我正在使用 HealthKit 框架来检索使用 HKSource 的用户的步骤。 在 Xcode Objective C 中,我使用 Bundle Identifier 来区分 watch/iPhone 的步骤。但是使用 Swift 我无法做到这一点。请建议。
提前致谢。
【问题讨论】:
标签: ios swift apple-watch healthkit hksamplequery
我正在使用 HealthKit 框架来检索使用 HKSource 的用户的步骤。 在 Xcode Objective C 中,我使用 Bundle Identifier 来区分 watch/iPhone 的步骤。但是使用 Swift 我无法做到这一点。请建议。
提前致谢。
【问题讨论】:
标签: ios swift apple-watch healthkit hksamplequery
在查询选项中使用.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)
【讨论】: