Can't properly check if Health Kit is authorized.

In my WatchOS app I've written the following code to check if my app has access to the user's health data:

func isHealthKitAuthorized() -> Bool {
        let typesToRead: [HKObjectType] = [
            HKObjectType.quantityType(forIdentifier: .heartRate)!,
            HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
            HKObjectType.quantityType(forIdentifier: .appleMoveTime)!,
            HKObjectType.quantityType(forIdentifier: .appleExerciseTime)!,
            HKObjectType.workoutType()
        ]
        
        let typesToShare: Set<HKSampleType> = [
            HKObjectType.workoutType(),
            HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
            HKObjectType.quantityType(forIdentifier: .heartRate)!
        ]
        
        var isAuthorized = true
        
        for type in typesToRead {
            let status = healthStore.authorizationStatus(for: type)
            if status != .sharingAuthorized {
                print("Access denied to: \(type.identifier)")
                isAuthorized = false
            }
        }
        
        for type in typesToShare {
            let status = healthStore.authorizationStatus(for: type)
            if status != .sharingAuthorized {
                print("Access denied to: \(type.identifier)")
                isAuthorized = false
            }
        }

        return isAuthorized
    }

However for the appleMoveTime and appleExerciseTime types their status is returning as 'sharingDenied' (checked by printing the status' rawValue) even though they are authorized on the Watch's settings. This happened both on the simulator and on the Watch itself. Am I doing something wrong?

In your code snippet though, I don't see that .appleMoveTime and .appleExerciseTime are included in typesToShare, and so it may be worth checking if your app indeed successfully requests the authorization.

If the settings (Settings > Health > Apps > Your app) on your watch shows that your app is allowed to write the data types but healthStore.authorizationStatus returns you sharingDenied, that will be a system bug because the two things should be consistent, and I’d suggest that you file a feedback report with screenshots and code snippets that demonstrate the issue – If you do so, please share your report ID here for folks to track.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Can't properly check if Health Kit is authorized.
 
 
Q