Request data from HealthKit on Apple Watch

I have a clock set app that tries to request HealthKit permission from an interface controller on an Apple watch, however, when I launch the app, I never asked for permission, and the app does not have a permission to work by default. How can I make an Apple HealthKit request for an Apple request?

+4
source share
1 answer

You can request access to the health kit from your watchOS application, but the user will need to accept the request on their iPhone. To do this, you request access to the health set of your watchOS application, as usual:

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
}

AppDelegate iOS:

func applicationShouldRequestHealthAuthorization(application: UIApplication) {
    let healthStore = HKHealthStore()
    healthStore.handleAuthorizationForExtensionWithCompletion { (success, error) -> Void in
        //...
    }
}

, , , iPhone. iPhone, . iPhone, requestAuthorizationToShareTypes Apple.

+14

All Articles