Overview of Issue
My implementation of HealthKit is no longer able to read values due to authorization issues (ex. "HealthKitService: Not authorized to read HKQuantityTypeIdentifierHeight. Status: 0"). I have been through every conceivable debugging step including building a minimal project that just requests HealthKit data and the issue has persisted. I've tried my personal as well as Organizational developer teams. My MacOS and Mac Mini. Simulator and personal device. Rechecked entitlements, reprovisioned certificates. This makes no sense. And I have been unable to find anything similar in the Developer forums or documentation.
The problem occurs during the onboarding flow when the app requests HealthKit permissions. Even when the user grants permission in the HealthKit authorization sheet, the authorizationStatus
for characteristic data types (like Biological s3x and Date of Birth) and quantity data types (like Height and Weight) consistently returns as .sharingDenied
. This prevents the app from pre-filling the user's profile with their HealthKit data, forcing them to enter it manually.
The issue seems to be environmental rather than a specific code bug, as it has been reproduced in a minimal test case app and persists despite extensive troubleshooting.
Minimal test project: https://github.com/ChristopherJones72521/HealthKitTestApp**
STEPS TO REPRODUCE
Build app, attempt to sign in. No data is imported into the respective fields in the main app. Console logs confirm.
PLATFORM AND VERSION
iOS
Development environment: Xcode Version 16.4 (16F6), macOS 15.5 (24F74)
Run-time configuration: iOS 18.5
Relevant Code Snippets
Here are the key pieces of code that illustrate the implementation and the problem:
1. Requesting HealthKit Permissions (HealthKitService.swift)
This function is called to request authorization for the required HealthKit data types. The typesToRead
and typesToWrite
are defined in a centralized HealthKitTypes
struct.
// HealthKitService.swift
func requestPermissions(completion: @escaping (Bool, Error?) -> Void) {
guard HKHealthStore.isHealthDataAvailable() else {
completion(false, HealthKitError.notAvailable)
return
}
let typesToRead: Set<HKObjectType> = [
HKObjectType.characteristicType(forIdentifier: .dateOfBirth)!,
HKObjectType.characteristicType(forIdentifier: .biologicals3x)!,
HKObjectType.quantityType(forIdentifier: .height)!,
HKObjectType.quantityType(forIdentifier: .bodyMass)!
]
let typesToWrite: Set<HKSampleType> = [
HKObjectType.workoutType(),
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!
]
healthStore.requestAuthorization(toShare: typesToWrite, read: typesToRead) { success, error in
DispatchQueue.main.async {
if let error = error {
print("HealthKitService: Error requesting authorization: \(error.localizedDescription)")
completion(false, error)
} else {
print("HealthKitService: Authorization request completed. Success: \(success)")
completion(success, nil)
}
}
}
}
2. Reading Biological s3x (HealthKitService.swift)
This function attempts to read the user's biological s3x. The print statements are included to show the authorization status check, which is where the issue is observed.
// HealthKitService.swift
func readBiologicals3x() async throws -> HKBiologicals3xObject? {
guard HKHealthStore.isHealthDataAvailable() else { throw HealthKitError.notAvailable }
let s3xAuthStatus = healthStore.authorizationStatus(for: HKObjectType.characteristicType(forIdentifier: .biologicals3x)!)
print("HealthKitService: Auth status for Biological s3x: \(s3xAuthStatus.rawValue)")
guard s3xAuthStatus == .sharingAuthorized else {
print("HealthKitService: Not authorized to read Biological s3x.")
throw HealthKitError.notAuthorized
}
do {
return try healthStore.biologicals3x()
} catch {
print("HealthKitService: Error executing biologicals3x query: \(error.localizedDescription)")
throw HealthKitError.queryFailed(error)
}
}
3. Calling HealthKit Functions During Onboarding (OnboardingFlowView.swift)
This is how the HealthKitService
is used within the onboarding flow. The requestHealthKitAndPrefillData
function is called after the user signs in, and it attempts to read the data to pre-fill the profile form.
// OnboardingFlowView.swift
func readHealthKitDataAsync() async {
print("Attempting to read HealthKit data async...")
// ... (calls to HealthKitService.shared.readDateOfBirth(), readHeight(), etc.)
do {
if let biologicals3xObject = try await HealthKitService.shared.readBiologicals3x() {
if self.selectedGender == nil {
switch biologicals3xObject.biologicals3x {
case .female: self.selectedGender = .female
case .male: self.selectedGender = .male
case .other: self.selectedGender = .other
default:
break
}
}
}
} catch {
print("OnboardingFlowView: Error reading Biological s3x: (error.localizedDescription)")
}
print("OnboardingFlowView: Finished HealthKit data processing.")
}
Console Logs
Attempting to read HealthKit data async...
HealthKitService: Reading Date of Birth...
HealthKitService: Current auth status for DOB (during read attempt): 0
HealthKitService: Not authorized to read Date of Birth. Status: 0
OnboardingFlowView: Error reading Date of Birth: The operation couldn’t be completed. (Strike_Force.HealthKitError error 2.)
HealthKitService: Reading Height...
HealthKitService: Current auth status for HKQuantityTypeIdentifierHeight (during read attempt): 0
HealthKitService: Not authorized to read HKQuantityTypeIdentifierHeight. Status: 0
OnboardingFlowView: Error reading Height: The operation couldn’t be completed. (Strike_Force.HealthKitError error 2.)
HealthKitService: Reading Weight (Body Mass)...
HealthKitService: Current auth status for HKQuantityTypeIdentifierBodyMass (during read attempt): 0
HealthKitService: Not authorized to read HKQuantityTypeIdentifierBodyMass. Status: 0
OnboardingFlowView: Error reading Weight: The operation couldn’t be completed. (Strike_Force.HealthKitError error 2.)
HealthKitService: Pre-read check for Biologicals3x auth status: 1 (Denied)
HealthKitService: Reading Biological s3x...
HealthKitService: Current auth status for Biological s3x (during read attempt): 1
HealthKitService: Not authorized to read Biological s3x. Status: 1
OnboardingFlowView: Error reading Biological s3x: The operation couldn’t be completed. (Strike_Force.HealthKitError error 2.)
I think there's a misunderstanding here on what sharingDenied
status means. That status merely implies that your app is not allowed to write the data type. It makes no assertion on your apps ability to read the data type. See this page which says
To help maintain the privacy of sensitive health data, HealthKit does not tell you when the user denies your app permission to query data. Instead, it simply appears as if HealthKit does not have any data matching your query.
Getting .sharingDenied
for the data type makes sense for 2 reasons
- Your app did not request write access to it
- The data type is read only so even if you did, your app would not have access to write that data type to HealthKit
In your case, there isn't a way to tell if your app can or cannot read the data. You can only tell that your app has requested authorization for the data types you need and a further call to requestAuthorization
will not show the UI prompt. See getRequestStatusForAuthorization .
I'd recommend that instead of checking for .sharingAuthorized
, you should just check for != .notDetermined
and execute the query if the status isn't that. When I update the test app with that change, I could see that the app read and printed the value returned from HealthKit.