I have a watch kit app that tries to request HealthKit permission from the interface controller on Apple watch, however when I run the app I am never asked for permission and the app defaults to not having health kit permission. How can I make the apple request HealthKit permission ?
You can request health kit access from your watchOS app, but the user will need to accept the request on their iPhone. To do this, you request access to health kit on your watchOS app like normal:
var healthKitTypesToRead = Set<HKObjectType>()
// TODO: add your read types here
var healthKitTypesToWrite = Set<HKSampleType>()
// TODO: add your write types here
healthStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in
// TODO: Handle success or failure
}
And then you need to add this to your AppDelegate in your iOS app:
func applicationShouldRequestHealthAuthorization(application: UIApplication) {
let healthStore = HKHealthStore()
healthStore.handleAuthorizationForExtensionWithCompletion { (success, error) -> Void in
//...
}
}
Now when you request health kit permissions from the user on the watch, they will get a permission dialog on the watch telling them to accept or deny permissions on their iPhone. When they go to their iPhone, your app's health kit permission dialog will be waiting. Once they have set their preferences on the iPhone, your requestAuthorizationToShareTypes completion handler will be called on your apple watch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With