我想要分别跑步和行走的全部步骤
方法1
我能够使用
获取步骤let healthStore = HKHealthStore()
func getTodaysSteps(completion: @escaping (Double) -> Void) {
let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)
let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { _, result, _ in
guard let result = result, let sum = result.sumQuantity() else {
completion(0.0)
return
}
completion(sum.doubleValue(for: HKUnit.count()))
}
healthStore.execute(query)
}
方法2
我也尝试过使用CoreMotion
,
CMPedometer
处,我可以使用pedoMeter.queryPedometerData(from: to:)
和
使用CMMotionActivityManager
的{{1}},我可以得到诸如步行跑步等活动类型。
我不确定如何将其结合起来,也不确定准确性的保证
提前谢谢!