HealthKit错误"受保护的健康数据无法访问"?

时间:2018-02-09 20:38:35

标签: ios swift health-kit

在尝试从iPhone上的HealthKit加载锻炼数据时,我偶尔遇到(并非每次都是)错误:Error Domain=com.apple.healthkit Code=6 "Protected health data is inaccessible" {NSLocalizedDescription=Protected health data is inaccessible}

我发现此错误的唯一其他参考是when attempting to access HealthKit data when the phone is locked 但我试图在手机解锁的情况下加载数据。还有其他想法可能是什么问题?

My HealthKit Permissions:

private func requestAccessToHealthKit() {
        let healthStore = HKHealthStore()

        let healthKitTypesToWrite: Set<HKSampleType> = [HKObjectType.workoutType(),
            HKSeriesType.workoutRoute(),
            HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
            HKObjectType.quantityType(forIdentifier: .heartRate)!,
            HKObjectType.quantityType(forIdentifier: .restingHeartRate)!,
            HKObjectType.quantityType(forIdentifier: .bodyMass)!,
            HKObjectType.quantityType(forIdentifier: .vo2Max)!,
            HKObjectType.quantityType(forIdentifier: .stepCount)!,
            HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!]

        let healthKitTypesToRead: Set<HKObjectType> = [
            HKObjectType.workoutType(),
            HKSeriesType.workoutRoute(),
            HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
            HKObjectType.quantityType(forIdentifier: .heartRate)!,
            HKObjectType.quantityType(forIdentifier: .restingHeartRate)!,
            HKObjectType.characteristicType(forIdentifier: .dateOfBirth)!,
            HKObjectType.quantityType(forIdentifier: .bodyMass)!,
            HKObjectType.quantityType(forIdentifier: .vo2Max)!,
            HKObjectType.quantityType(forIdentifier: .stepCount)!,
            HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!]


        healthStore.requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (success, error) in
            if !success {
                print("failed HealthKit Authorization from iPhone \(String(describing: error?.localizedDescription))")
            }

            print("Successful HealthKit Authorization from iPhone")
        }
    }

0 个答案:

没有答案