iOS HealthKit 在会话期间跟踪用户步数
iOS HealthKit track user steps during session
我正在尝试跟踪用户在会话期间采取的步骤。这些会话可以是 30 秒到 30 分钟不等。我开始课程并开始四处走动,然后一分钟后我停止了课程。这总是 returns 0 步。这是我的代码 运行.
func readUserSteps(startDate:NSDate, endDate:NSDate) {
let weightSampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)
let query = HKSampleQuery(sampleType: weightSampleType!, predicate: predicate, limit: 0, sortDescriptors: nil, resultsHandler: {
(query, results, error) in
if results == nil {
print("There was an error running the query: \(error)")
} else {
var stepCount:Double = 0
for steps in results as! [HKQuantitySample]
{
stepCount += steps.quantity.doubleValueForUnit(HKUnit.countUnit())
}
print("Steps Taken: \(stepCount)")
}
})
self.healthKitStore.executeQuery(query)
}
我将当前日期保存为:let startTime = NSDate()
以表示会话的开始,一旦用户停止会话,我将会话结束保存为:let endTime = NSDate()
。然后我用创建的这两个变量调用上面的函数。不幸的是,'stepCount' 总是返回 0..我在这里做错了什么?
如果您只对计算用户 iPhone 或 Apple Watch 记录的步数感兴趣,我建议使用 CMPedometer API instead as it will always have the most up-to-date records. If you'd still like to use HealthKit, though, you will need to indicate to HealthKit that your app is interested in "observing" steps by executing any of the query types that has an updateHandler
property (HKObserverQuery, HKStatisticsCollectionQuery, or HKAnchoredObjectQuery)。在会话开始时使用更新处理程序执行这些查询之一,以使 HealthKit 更频繁地导入步数。
HKObserverQuery
只会在 HealthKit 中有新的步数时通知您,因此在会话结束时您需要重新查询总数。另一方面,HKStatisticsCollectionQuery
和 HKAnchoredObjectQuery
可以在会话为 运行 时将结果流式传输到您的应用程序,这可能会提高性能。
最后,请注意简单地对 HKQuantitySamples
的值求和以计算总步数。来自多个来源的 HealthKit 数据可以在时间上重叠。例如,当 Apple Watch 用户戴着手表并携带 phone 时,他们可以在一天中的 HealthKit 中拥有多个步数来源。因此,最好使用 HKStatisticsQuery
或 HKStatisticsCollectionQuery
来计算总和,因为它可以避免重复计算重叠样本。
我正在尝试跟踪用户在会话期间采取的步骤。这些会话可以是 30 秒到 30 分钟不等。我开始课程并开始四处走动,然后一分钟后我停止了课程。这总是 returns 0 步。这是我的代码 运行.
func readUserSteps(startDate:NSDate, endDate:NSDate) {
let weightSampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)
let query = HKSampleQuery(sampleType: weightSampleType!, predicate: predicate, limit: 0, sortDescriptors: nil, resultsHandler: {
(query, results, error) in
if results == nil {
print("There was an error running the query: \(error)")
} else {
var stepCount:Double = 0
for steps in results as! [HKQuantitySample]
{
stepCount += steps.quantity.doubleValueForUnit(HKUnit.countUnit())
}
print("Steps Taken: \(stepCount)")
}
})
self.healthKitStore.executeQuery(query)
}
我将当前日期保存为:let startTime = NSDate()
以表示会话的开始,一旦用户停止会话,我将会话结束保存为:let endTime = NSDate()
。然后我用创建的这两个变量调用上面的函数。不幸的是,'stepCount' 总是返回 0..我在这里做错了什么?
如果您只对计算用户 iPhone 或 Apple Watch 记录的步数感兴趣,我建议使用 CMPedometer API instead as it will always have the most up-to-date records. If you'd still like to use HealthKit, though, you will need to indicate to HealthKit that your app is interested in "observing" steps by executing any of the query types that has an updateHandler
property (HKObserverQuery, HKStatisticsCollectionQuery, or HKAnchoredObjectQuery)。在会话开始时使用更新处理程序执行这些查询之一,以使 HealthKit 更频繁地导入步数。
HKObserverQuery
只会在 HealthKit 中有新的步数时通知您,因此在会话结束时您需要重新查询总数。另一方面,HKStatisticsCollectionQuery
和 HKAnchoredObjectQuery
可以在会话为 运行 时将结果流式传输到您的应用程序,这可能会提高性能。
最后,请注意简单地对 HKQuantitySamples
的值求和以计算总步数。来自多个来源的 HealthKit 数据可以在时间上重叠。例如,当 Apple Watch 用户戴着手表并携带 phone 时,他们可以在一天中的 HealthKit 中拥有多个步数来源。因此,最好使用 HKStatisticsQuery
或 HKStatisticsCollectionQuery
来计算总和,因为它可以避免重复计算重叠样本。