我正在为Apple Watch编写Running-Workout-App,在1km之后,app会测量HeartRate并将其与特定值进行比较。之后,如果Heartrate为高,则会显示警报。
我的问题:
答案 0 :(得分:1)
您可以监控距离更新,并使用HKSampleQuery在用户达到1KM时进行最后一次心率测量。
let healthStore = HKHealthStore()
let heartrate = HKQuantityType.quantityType(forIdentifier:HKQuantityTypeIdentifier.heartRate)
let sort = [
NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
]
let heartRateUnit = HKUnit(from: "count/min")
let sampleQuery = HKSampleQuery(sampleType: heartrate!, predicate: nil, limit: 1, sortDescriptors: sort, resultsHandler: {
query, results, error in
if let results = results as? [HKQuantitySample]
{
let sample = results[0] as HKQuantitySample
let value = sample.quantity.doubleValue(for: heartRateUnit)
print (value)
let rate = results[0]
print(rate)
}
})
healthStore.execute(sampleQuery)