Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Check if HealthKit has been authorized

Tags:

I want to check if HeathKit has been authorized for me to read the user's data, if I'm authorized segue to the workouts, if not pop an alert. But requestAuthorizationToShareTypes always seems to return true? How can I get a reference to whether the user has authorized 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()                                     });             }          }     } 

Alternatively, I've tried authorizationStatusForType and switched on its enum values but had the same problem in that I'm always authorized.

like image 518
GarySabo Avatar asked Oct 08 '15 14:10

GarySabo


People also ask

How do I enable Apple HealthKit?

Enable HealthKitIn Xcode, select the project and add the HealthKit capability. Only select the Clinical Health Records checkbox if your app needs to access the user's clinical records. App Review may reject apps that enable the Clinical Health Records capability if the app doesn't actually use the health record data.

How do I add entitlement to HealthKit?

To add this entitlement to your app, first enable the HealthKit capability in Xcode, and then check any values that you want to add to the HealthKit Capabilities Entitlement. Only add values for data types that your app needs to access. App Review may reject apps that don't use the data appropriately.

Where is HealthKit data stored?

To quote: This data is stored in Data Protection class Protected Unless Open. Access to the data is relinquished 10 minutes after device locks, and data becomes accessible the next time user enters their passcode or uses Touch ID or Face ID to unlock the device.

How does Apple HealthKit work?

It collects health and fitness data from your iPhone, the built-in sensors on your Apple Watch, compatible third-party devices, and apps that use HealthKit. The Health app is built to keep your data secure and protect your privacy. Your data is encrypted and you are always in control of your health information.


1 Answers

You are misinterpreting what the success flag means in this context. When success is true, all that means is that iOS successfully asked the user about health kit access. It does NOT mean that they responded to that question with a 'yes'.

To determine if they said yes/no, you need to get more specific, and ask health kit if you have permission to read/write the particular type of data you're interested in. From the apple docs on HealthKit:

After requesting authorization, your app is ready to access the HealthKit store. If your app has permission to share a data type, it can create and save samples of that type. You should verify that your app has permission to share data by calling authorizationStatusForType: before attempting to save any samples.

like image 188
Nick Avatar answered Oct 14 '22 07:10

Nick