An overview of HealthKit delivered at 360|iDev Min in Greenville, SC on October 7, 2014
Clean bill of HealthKitMaking the most of health dataJosh Johnson | Two Toasters | @jnjosh
View Slide
“I’m not adoctor. I don’teven play oneon TV.”— Me, just now.
“What is this, the DarkAges?”— Doctor Leonard “Bones” McCoy
“Hello Computer”— Scotty
“So, What is isthis HealthKitthing?”— You, hopefully.
The HealthKit Crash Course™
WWDC 2014 Session 203:Introducing HealthKithttps://developer.apple.com/videos/wwdc/2014/?id=203
HKHealthStoreself.healthStore = [[HKHealthStore alloc] init];
HKHealthStore// …[self.healthStore requestAuthorizationToShareTypes:writeTypesreadTypes:readTypescompletion:^(BOOL success, NSError *error) {// Do something!}];
HKObjectTypeHKQuantityType *stepType = [HKObjectTypequantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];NSSet *writeTypes = [NSSet setWithObject:stepType];NSSet *readTypes = [NSSet setWithObject:stepType];
HKObjectTypeHKQuantityType *stepType = [HKObjectTypequantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];NSSet *writeTypes = [NSSet setWithObject:stepType];NSSet *readTypes = [NSSet setWithObject:stepType];[self.healthStore requestAuthorizationToShareTypes:writeTypesreadTypes:readTypescompletion:^(BOOL success, NSError *error) {// Do something!}];
HKHealthStore[self.healthStore whatElseCanYouDo];
HKHealthStore — SavingHKQuantityType *stepQuantityType = [HKQuantityTypequantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];// …
HKUnitHKQuantityType *stepQuantityType = [HKQuantityTypequantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];HKQuantity *stepQuantity = [HKQuantity quantityWithUnit:[HKUnit countUnit]doubleValue:stepCount];// …
HKQuantitySampleHKQuantityType *stepQuantityType = [HKQuantityTypequantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];HKQuantity *stepQuantity = [HKQuantity quantityWithUnit:[HKUnit countUnit]doubleValue:stepCount];HKQuantitySample *stepSample = [HKQuantitySample quantitySampleWithType:stepQuantityTypequantity:stepQuantitystartDate:someStartDateendDate:someEndDate];
HKHealthStore — Saving// …[self.healthStore saveObject:stepSample withCompletion:^(BOOL success, NSError *error) {// You better check that error.}];
HKHealthStore — Querying
HKHealthStore — QueryingHKQuantityType *stepQuantityType = [HKQuantityTypequantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];//…
HKHealthStore — QueryingHKQuantityType *stepQuantityType = [HKQuantityTypequantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];NSPredicate *appPredicate = [HKSampleQuerypredicateForObjectsFromSource:[HKSource defaultSource]];//…
HKHealthStore — QueryingHKQuantityType *stepQuantityType = [HKQuantityTypequantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];NSPredicate *appPredicate = [HKSampleQuerypredicateForObjectsFromSource:[HKSource defaultSource]];HKSampleQuery *stepQuery = [[HKSampleQuery alloc] initWithSampleType:stepQuantityTypepredicate:appPredicatelimit:HKObjectQueryNoLimitsortDescriptors:nilresultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {// results will be delivered as HKSamples. In this case, HKQuantitySamples.// You should also handle that Error!}];//…
HKHealthStore — Querying//… stepQuery created[self.healthStore executeQuery:stepQuery];
“Let’s build somethingcool instead.”— All of us, just now.
Demo
With great powercomes greatresponsibility.
Thank you!Questions?Josh Johnson | Two Toasters | @jnjosh