I'm running into an occasional (not every time) error when attempting to load workout data from HealthKit on the iPhone: Error Domain=com.apple.healthkit Code=6 "Protected health data is inaccessible" {NSLocalizedDescription=Protected health data is inaccessible}
The only other references to this error I've found is when attempting to access HealthKit data when the phone is locked But I am attempting to load the data with the phone unlocked. Any other idea what the issue could be?
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")
}
}