healthkit

17
HealthKit Busra Deniz SWE-551, Fall 2015

Upload: busra-deniz-csm

Post on 16-Apr-2017

502 views

Category:

Software


0 download

TRANSCRIPT

Page 1: HealthKit

HealthKitBusra Deniz

SWE-551, Fall 2015

Page 2: HealthKit

HealthKit Overview● HealthKit allows apps that provide health and fitness

services to share their data with the new Health app and with each other.

● A user’s health information is stored in a centralized and secure location and the user decides which data should be shared with your app

● HealthKit is designed to manage data from a wide range of sources, automatically merging data from all different sources based on users’ preferences.

● Works with directly health and fitness devices.

Page 3: HealthKit

HealthKit Overview● Provides an app to help manage

users’ health data.

● View, add, delete and manage health and fitness data using this app

● Edit sharing permissions for each data type

● Both HealthKit and Health app are unavailable on iPad

● The HealthKit framework cannot be used in an app extension

Page 4: HealthKit

HealthKit and Privacy● The user must explicitly grant each app permission to read and write data

to HealthKit store. Users can grant or deny permission separately for each type of data

● The HealthKit data is only kept locally on the user’s device

● For security, the HealthKit store is encrypted when the device is not unlocked

● The HealthKit store can only be accessed by an authorized app. Not accessible from extensions or a WatchKit app

● Your app must not access the HealthKit APIs unless the app is primarily designed to provide health or fitness services

Page 5: HealthKit

HealthKit Benefits● Separating data collection, data processing, and

socialization

● Reduces friction in sharing between apps

● Providing a richer set of data and a greater sense of context

● Lets apps participate in a greater ecosystem

Page 6: HealthKit

HealthKit’s Design Philosophy● The framework constrains the types of data and units to a predefined

list. Developers cannot create custom data types or units

● All objects in the HealthKit store are subclasses of the HKObject class ○ UUID ○ Source ○ Metadata

● HealthKit objects can be divided into two main groups : ○ Characteristics

○ Samples

Page 7: HealthKit

HealthKit’s Design Philosophy● Characteristic objects represent data that typically does not change. This

data includes the user’s birthdate, blood type, and biological sex

● Sample objects represent data at a particular point in time. Subclasses of HKSample class ○ Type○ Start date

○ End date

● Samples can be further partitioned into four sample types : ○ Category samples○ Quantity samples○ Correlations○ Workouts

Page 8: HealthKit

Setting Up HealthKit

Page 10: HealthKit

Sample Codeimport HealthKitlet healthKitStore:HKHealthStore = HKHealthStore()

func authorizeHealthKit(completion: ((success:Bool, error:NSError!) -> Void)!){

// 1. Set the types you want to read from HK Store let healthKitTypesToRead = Set(arrayLiteral:[ HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth), HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType), HKObjectType.workoutType() ]) // 2. Set the types you want to write to HK Store let healthKitTypesToWrite = Set(arrayLiteral:[ HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned), HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning), HKQuantityType.workoutType() ]) // Continue

Page 11: HealthKit

Sample Code Continue // 3. 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.example.healthkit", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"]) if( completion != nil ) { completion(success:false, error:error) } return; } // 4. Request HealthKit authorization healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in if( completion != nil ) { completion(success:success,error:error) }

}}

Page 12: HealthKit

Sample Code ContinuehealthManager.authorizeHealthKit { (authorized, error) -> Void in

if authorized { println("HealthKit authorization received.") } else { println("HealthKit authorization denied!") if error != nil { println("\(error)") } }

}

Page 13: HealthKit

Read Characteristicsfunc readCharacteristics() -> ( biologicalsex:HKBiologicalSexObject?, bloodtype:HKBloodTypeObject?) { var error:NSError?

// Read biological sex var biologicalSex:HKBiologicalSexObject? = healthKitStore.biologicalSexWithError(&error); if error != nil { println("Error reading Biological Sex: \(error)") }

// Read blood type var bloodType:HKBloodTypeObject? = healthKitStore.bloodTypeWithError(&error); if error != nil { println("Error reading Blood Type: \(error)") } // Return the information return (biologicalSex, bloodType)}

Page 14: HealthKit

Samples Queryfunc readMostRecentSample(sampleType:HKSampleType , completion: ((HKSample!, NSError!) -> Void)!){ // 1. Build the Predicate let past = NSDate.distantPast() as! NSDate let now = NSDate() let mostRecentPredicate = HKQuery.predicateForSamplesWithStartDate(past, endDate:now, options: .None) // 2. Build the sort descriptor to return the samples in descending order let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false) // 3. we want to limit the number of samples returned by the query to just 1 (the most recent) let limit = 1 // 4. Build samples query let sampleQuery = HKSampleQuery(sampleType: sampleType, predicate: mostRecentPredicate, limit: limit, sortDescriptors: [sortDescriptor]) { (sampleQuery, results, error ) -> Void in if let queryError = error { completion(nil,error) return; } // Get the first sample let mostRecentSample = results.first as? HKQuantitySample // Execute the completion closure if completion != nil { completion(mostRecentSample,nil) } } // 5. Execute the Query self.healthKitStore.executeQuery(sampleQuery)}

Page 15: HealthKit

Save New Samplesfunc saveBMISample(bmi:Double, date:NSDate ) { // 1. Create a BMI Sample let bmiType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex) let bmiQuantity = HKQuantity(unit: HKUnit.countUnit(), doubleValue: bmi) let bmiSample = HKQuantitySample(type: bmiType, quantity: bmiQuantity, startDate: date, endDate: date) // 2. Save the sample in the store healthKitStore.saveObject(bmiSample, withCompletion: { (success, error) -> Void in if( error != nil ) { println("Error saving BMI sample: \(error.localizedDescription)") } else { println("BMI sample saved successfully!") } })}