Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requesting Data From HealthKit on Apple Watch

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 ?

like image 461
loadedjd Avatar asked Mar 13 '16 16:03

loadedjd


1 Answers

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.

like image 177
lehn0058 Avatar answered Oct 02 '22 15:10

lehn0058