iOS Swift,如何使用Apple Watch和iPhone区分步骤?



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

提前谢谢。

在查询选项中使用.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)

相关内容

最新更新