本文介绍了向Healthstore查询静息心率未返回任何值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用下面的代码在锻炼过程中获取心率没有问题,但是在查询自己的心率时(我知道健康"应用程序中有值),我没有得到任何结果吗?我做错什么了吗?
I have no trouble getting heart rates during a workout with the code below, however when querying my own heart rates (and I know there are values in the Health app) I am getting no results? Am I doing anything wrong?
func getuserRestingHeartRate(completion: @escaping (HKQuantitySample) -> Void) {
guard let restingHeartRateSampleType = HKSampleType.quantityType(forIdentifier: .restingHeartRate) else {
print("Resting Heart Rate Sample Type is no longer available in HealthKit")
return
}
//1. Use HKQuery to load the most recent samples.
let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast,
end: Date(),
options: .strictEndDate)
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
//let limit = 1
let sampleQuery = HKSampleQuery(sampleType: restingHeartRateSampleType,
predicate: mostRecentPredicate,
limit: HKObjectQueryNoLimit,
sortDescriptors:
[sortDescriptor]) { (query, samples, error) in
DispatchQueue.main.async {
guard let samples = samples,
let mostRecentSample = samples.first as? HKQuantitySample else {
print("getUserRestingHeartRate sample is missing")
return
}
completion(mostRecentSample)
}
}
HKHealthStore().execute(sampleQuery)
}
推荐答案
结果证明restingHeartRate
具有其自己的权限(除了heartRate
之外):
Turns out restingHeartRate
has its own permission that is required (in addition to heartRate
):
private func requestAccessToHealthKit() {
let healthStore = HKHealthStore()
let allTypes = Set([HKObjectType.quantityType(forIdentifier: .heartRate)!,
HKObjectType.quantityType(forIdentifier: .restingHeartRate)!,
healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
if !success {
}
}
}
这篇关于向Healthstore查询静息心率未返回任何值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!