Health Kit 按一天的运动步数按设备类型分隔
Health Kit steps by movement for the day separated by device type
如何获取如下图所示的数据 Swift,我正在计算每天的步数和每小时的步数,但不确定如何获取如下图所示的数据。我想通过设备(手表,phone)获得每个运动组的步数而不是时间间隔?
我使用了 HKSampleQuery
func getStepCountForTodaySegmentedByMovement( complete: @escaping ([HKCumulativeQuantitySample]) -> () ) {
let healthStore = HKHealthStore()
guard let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount) else {
fatalError("*** Unable to create a step count type ***")
}
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: .distantFuture, options: .strictEndDate)
let query = HKSampleQuery(sampleType: stepsQuantityType,
predicate: predicate,
limit: HKObjectQueryNoLimit,
sortDescriptors: [NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: true)]) {
(query, results, error) in
complete(results as? [HKCumulativeQuantitySample] ?? [])
}
healthStore.execute(query)
}
如果您想要像“健康”应用程序显示的那样的单个示例,示例查询就可以使用。相关点,如果它有用,如果您传递 HKStatisticsOptions separateBySource
,您还可以获得按来源分隔的统计信息
如何获取如下图所示的数据 Swift,我正在计算每天的步数和每小时的步数,但不确定如何获取如下图所示的数据。我想通过设备(手表,phone)获得每个运动组的步数而不是时间间隔?
我使用了 HKSampleQuery
func getStepCountForTodaySegmentedByMovement( complete: @escaping ([HKCumulativeQuantitySample]) -> () ) {
let healthStore = HKHealthStore()
guard let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount) else {
fatalError("*** Unable to create a step count type ***")
}
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: .distantFuture, options: .strictEndDate)
let query = HKSampleQuery(sampleType: stepsQuantityType,
predicate: predicate,
limit: HKObjectQueryNoLimit,
sortDescriptors: [NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: true)]) {
(query, results, error) in
complete(results as? [HKCumulativeQuantitySample] ?? [])
}
healthStore.execute(query)
}
如果您想要像“健康”应用程序显示的那样的单个示例,示例查询就可以使用。相关点,如果它有用,如果您传递 HKStatisticsOptions separateBySource
,您还可以获得按来源分隔的统计信息