1

I want to be able to tell if the user has allowed my app to read their health data. I found some code that seems to be doing just that, however, whenever I call the function, it always prints "Denied", instead of "Authorized", even though I am allowing it on my test device. Here's my code:

func checkAuthorization() {
    let authorizationStatus = healthStore.authorizationStatus(for: HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)!)
    if authorizationStatus == HKAuthorizationStatus.sharingAuthorized {
        print("Authorized")
    } else if authorizationStatus == HKAuthorizationStatus.sharingDenied {
        print("Denied")
    } else {
        print("Unknown")
    }
}
aequinox
  • 125
  • 1
  • 17
  • This is a little confusing, but it's behaving as designed. Check out the existing StackOverlow answer here: https://stackoverflow.com/questions/29076655/healthkit-authorisation-status-is-always-1/29128231#29128231 – Allan Mar 14 '19 at 16:10
  • @Allan Thanks -- so is there any way for me to be able to tell if I have been granted permission? Because then I would want to display a message asking the user to go into settings and allow me to read their health data. – aequinox Mar 15 '19 at 17:04
  • Correct, by design you cannot tell whether the user has granted read authorization. – Allan Mar 16 '19 at 18:03
  • @Allan OK - thanks for the help. I know Apple says that the purpose is privacy, but I don't understand why knowing whether or not I even have data is a matter of personal privacy... – aequinox Mar 18 '19 at 14:33

0 Answers0