Before you can use information in Healthkit available on iOS devices, you must first get access permission from the mobile user. Apple provides a method for the user to grant authorization to access to their Healthkit data in iOS which can be implemented in a few simple steps.
1. First we need to setup a controller that is going to grant access.
// First we need to import Healthkit import UIKit import HealthKit class ViewController: UIViewController { // Healthkit Access Object let healthkitStore:HealthkitStore = HKHealthStore() }
2. Next we are going to add a method to our class that actually performs the authorization.
// Create a new method func authorizeHealthKit(completion: ((success: Bool, error:NSError!) -> Void)!) { // We need to define an array of variables that we need authorization to read // this will be passed into requestAuthorization function. let healthkitTypesToRead = NSSet(array: [ // Each type we need access to is going to be an HKObjectType. // Here is are getting the Sex, BloodType and StepCount information. HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex), HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType), HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth), HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount), ]) // We also need to define the information that we will write to. // Just as the read array we define a set of HKObjectType. let healthkitTypesToWrite = NSSet(array: [ // Here we are only requesting access to write for the amount of Energy Burned. HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned), ]) // Before we actually check anything we need to ensure that HealthKit is available on the device. if !HKHealthStore.isHealthDataAvailable() { let error = NSError(domain: "ai.Swift-Learning-App", code: 2, userInfo: [ NSLocalizedDescriptionKey:"HealthKit data is not available on this device."]) if (completion != nil) { completion(success:false, error:error) } return; } // This is where the actual authorization takes place. // We simply call the healthkitStore requestAuthorization function providing the // Arrays of types to read and write we set above and the User will be prompted // with the iOS authorization screen allowing them to set Yes or No for each // individual type we are requesting access to. healthKitStore.requestAuthorizationToShareTypes(healthkitTypesToWrite as Set, readTypes: healthkitTypesToRead as Set) { (success, error) -> Void in if (completion != nil) { completion(success: false,error:error) } } }
Once we have this method built into our controller we just need to call it.
self.authorizeHealthKit { (authorized, error) -> Void in // If authorization is received a nil error will be returned. if error == nil { println("health auth recieved.") } else { println("health auth denied.") println("\(error)") } }
After this method is called the user will be prompted with the authorization screen and their choices are automatically saved onto their device so they won’t need to grant the access again.