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?