HealthKit permissions not honoring user selection

I'm dealing with a strange bug where I am requesting read access for 'appleExerciseTime' and 'activitySummaryType', and despite enabling both in the permission sheet, they are being set to 'sharingDenied'.

I'm writing a Swift Test for making sure permissions are being granted.

@Test
func PermissionsGranted() {
    try await self.manager.getPermissions()

    for type in await manager.allHealthTypes {
        let status = await manager.healthStore.authorizationStatus(for: type)
        #expect(status == .sharingAuthorized, "\(type) authorization status is \(status)")
    }
}
let healthTypesToShare: Set<HKSampleType> = [
    HKQuantityType(.bodyMass),
    HKQuantityType(.bodyFatPercentage),
    HKQuantityType(.leanBodyMass),
    HKQuantityType(.activeEnergyBurned),
    HKQuantityType(.basalEnergyBurned),
    HKObjectType.workoutType()
]
let allHealthTypes: Set<HKObjectType> = [
    HKQuantityType(.bodyMass),
    HKQuantityType(.bodyFatPercentage),
    HKQuantityType(.leanBodyMass),
    HKQuantityType(.activeEnergyBurned),
    HKQuantityType(.basalEnergyBurned),
    HKQuantityType(.appleExerciseTime),
    HKObjectType.activitySummaryType()
]
let healthStore = HKHealthStore()

func getPermissions() async throws {
    try await healthStore.requestAuthorization(toShare: self.healthTypesToShare, read: self.allHealthTypes)
}

After 'getPermissions' runs, the permission sheet shows up on the Simulator, and I accept all. I've double checked that the failing permissions show up on the sheet and are enabled. Then the test fails with:

Expectation failed: (status → HKAuthorizationStatus(rawValue: 1)) == (.sharingAuthorized → HKAuthorizationStatus(rawValue: 2)) HKActivitySummaryTypeIdentifier authorization status is HKAuthorizationStatus(rawValue: 1)

Expectation failed: (status → HKAuthorizationStatus(rawValue: 1)) == (.sharingAuthorized → HKAuthorizationStatus(rawValue: 2)) HKActivitySummaryTypeIdentifier authorization status is HKAuthorizationStatus(rawValue: 1)

With the rawValue of '1' being 'sharingDenied'. All other permissions are granted. Is there a workaround here, or something I'm potentially doing wrong?

Answered by DTS Engineer in 819854022

Would you mind to be clear what HealthKit types hitting .sharingDenied? If they are .appleExerciseTime and .activitySummaryType, the behavior will be as-designed because they are not in healthTypesToShare, as shown in your code snippet.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

Would you mind to be clear what HealthKit types hitting .sharingDenied? If they are .appleExerciseTime and .activitySummaryType, the behavior will be as-designed because they are not in healthTypesToShare, as shown in your code snippet.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

HealthKit permissions not honoring user selection
 
 
Q