【问题标题】:Query to Healthstore for Resting Heart Rate not returning any values向 Healthstore 查询静息心率未返回任何值
【发布时间】:2018-01-22 22:08:51
【问题描述】:

我在锻炼期间使用下面的代码可以毫无问题地获取心率,但是在查询我自己的心率时(并且我知道健康应用程序中有值)我没有得到任何结果?我做错什么了吗?

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)
}

【问题讨论】:

    标签: ios swift healthkit


    【解决方案1】:

    原来restingHeartRate 有自己需要的权限(除了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 {
    
                }
    
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      相关资源
      最近更新 更多