AppsDev
AppsDev

Reputation: 12509

Allowing access to Health: how can I know in my app that the user enabled/disabled it later?

When you call to healthStore.requestAuthorization method and user doesn't allow the access the first time he runs the app and the dialog is presented, how could I observe in my app if he allows the access later on the Health app, in order to try to request again the information to show in my app?

The only notification to subscribe I've found in the documentation is HKUserPreferencesDidChange and it does not talk about the authorization status, but:

Notifies observers whenever the user changes his or her preferred units.

Upvotes: 0

Views: 513

Answers (2)

Allan
Allan

Reputation: 7363

By design, you can’t tell whether the user has granted read access to HealthKit data. To determine whether your app has write access, use authorizationStatusForType: on HKHealthStore.

Upvotes: 1

nhiddink
nhiddink

Reputation: 235

You don't necessarily need to implement anything out of HealthKit to address this issue. Have you tried creating and checking a Boolean value, i.e. isAuthorized, to handle the status? You could check this in viewDidAppear(_:) of your view controller and then call healthStore.requestAuthorization if isAuthorized is false.

Also, if the user has already dismissed the initial HealthKit request, you could segue to a new view controller with instructions for how to allow access through the Settings app on the user's iPhone.

Upvotes: 1

Related Questions