简体   繁体   中英

Requesting authorization in HealthKit: why the result is always successful?

I´m requesting access to certain health info just for reading this way:

let healthStore = HKHealthStore()
let types: Set<HKObjectType> = [distanceWalkingRunning]

healthStore.requestAuthorization(toShare: nil, read: types) { (success, error) in
        completion(success, error)
}

Then, when launching the app the first time, I've been shown the standard dialog to choose if you want to allow Health to access the info or not. If I tap "Don't Allow", then an alert view is displayed saying:

Health Access - You can turn on health data categories later in the Health app.

Them, both the alert and the Health access dialog are dismissed and app's landing screen is shown. I can see that the completion closure provided in healthStore.requestAuthorization method call is getting a true value for the success argument and a nil for the error argument anyway.

How could I then know that the user didn't allow the access to Health?

The documentation for the success parameter reads:

A Boolean value that indicates whether the request was processed successfully. This value does not indicate whether permission was actually granted. This parameter is false if an error occurred while processing the request; otherwise, it is true .

In both the initial and subsequent requests, the request was processed successfully and you get true for success .

You cannot find out whether the user has granted read access to the data you requested. Again, from the documentation :

To help prevent possible leaks of sensitive health information, your app cannot determine whether or not a user has granted permission to read data. If you are not given permission, it simply appears as if there is no data of the requested type in the HealthKit store.

After presenting the authorisation request you simply try and read the data and you will either get it or there will appear to be no data available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM