How to check if HealthKit is allowed

I want to check if Heathkit was allowed for me to read user data, if I am an authorized SEGUE for training, if I do not pop a warning. But does requestAuthorizationToShareTypes always seem to return true? How can I get a link to whether the user allowed me or not?

override func viewDidLoad() { super.viewDidLoad() //1. Set the types you want to read from HK Store let healthKitTypesToRead: [AnyObject?] = [ HKObjectType.workoutType() ] //2. If the store is not available (for instance, iPad) return an error and don't go on. if !HKHealthStore.isHealthDataAvailable() { let error = NSError(domain: "com.myndarc.myrunz", code: 2, userInfo: [NSLocalizedDescriptionKey: "HealthKit is not available in this Device"]) print(error) let alertController = UIAlertController(title: "HealthKit Not Available", message: "It doesn't look like HealthKit is available on your device.", preferredStyle: .Alert) presentViewController(alertController, animated: true, completion: nil) let ok = UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in }) alertController.addAction(ok) } //3. Request Healthkit Authorization let sampleTypes = Set(healthKitTypesToRead.flatMap { $0 as? HKSampleType }) healthKitStore.requestAuthorizationToShareTypes(sampleTypes, readTypes: nil) { (success, error) -> Void in if success { dispatch_async(dispatch_get_main_queue(), { () -> Void in self.performSegueWithIdentifier("segueToWorkouts", sender: nil) }); } else { print(error) dispatch_async(dispatch_get_main_queue(), { () -> Void in self.showHKAuthRequestAlert() }); } } } 

As an alternative, I tried the authorization of StatususType and included its enumeration values, but had the same problem as I was always resolving.

+16
ios swift health-kit
source share
3 answers

You misinterpret what the success flag means in this context. When success true, all of this means that iOS has successfully asked the user to access a set of medical supplies. This does NOT mean that they answered yes to this question.

To determine if they said yes / no, you need to get more specific information and ask for a health kit if you have permission to read / write certain types of data that interest you. From apple docs in HealthKit:

After requesting authorization, your application will be available for access to the HealthKit store. If your application has permission to share a data type, it can create and save samples of this type. You must ensure that your application has permission to share data by calling authorizationStatusForType: before saving all the samples.

+9
source share

Note: authorizationStatus intended to determine read-only access status, but not read access. There is no way to find out if your application has read access. For your information, https://stackoverflow.com/a/318677/

Here is an example of requesting and verifying access rights in HealthKitStore

 // Present user with items we need permission for in HealthKit healthKitStore.requestAuthorization(toShare: typesToShare, read: typesToRead, completion: { (userWasShownPermissionView, error) in // Determine if the user saw the permission view if (userWasShownPermissionView) { print("User was shown permission view") // ** IMPORTANT // Check for access to your HealthKit Type(s). This is an example of using BodyMass. if (self.healthKitStore.authorizationStatus(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)!) == .sharingAuthorized) { print("Permission Granted to Access BodyMass") } else { print("Permission Denied to Access BodyMass") } } else { print("User was not shown permission view") // An error occurred if let e = error { print(e) } } }) 
+8
source share

Currently, the application cannot determine in any way whether the user has granted permission to read health data.

The following is Apple's description from authorizationStatus (for :) :

To prevent possible leakage of confidential health information, your application cannot determine if the user has granted permission to read the data. If you are not given permission, it just looks like there is no data of the requested type in the HealthKit store. If your application has been granted permission to share, but there is no read permission, you will see only the data that your application has written to the store. Data from other sources remains hidden.

0
source share

All Articles