Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clean Bill of HealthKit

Clean Bill of HealthKit

An overview of HealthKit delivered at 360|iDev Min in Greenville, SC on October 7, 2014

Josh Johnson

October 07, 2014
Tweet

More Decks by Josh Johnson

Other Decks in Programming

Transcript

  1. Clean bill of HealthKit
    Making the most of health data
    Josh Johnson | Two Toasters | @jnjosh

    View full-size slide

  2. “I’m not a
    doctor. I don’t
    even play one
    on TV.”
    — Me, just now.

    View full-size slide

  3. “What is this, the Dark
    Ages?”
    — Doctor Leonard “Bones” McCoy

    View full-size slide

  4. “Hello Computer”
    — Scotty

    View full-size slide

  5. “So, What is is
    this HealthKit
    thing?”
    — You, hopefully.

    View full-size slide

  6. The HealthKit Crash Course™

    View full-size slide

  7. WWDC 2014 Session 203:
    Introducing HealthKit
    https://developer.apple.com/videos/wwdc/2014/?id=203

    View full-size slide

  8. HKHealthStore
    self.healthStore = [[HKHealthStore alloc] init];

    View full-size slide

  9. HKHealthStore
    // …
    [self.healthStore requestAuthorizationToShareTypes:writeTypes
    readTypes:readTypes
    completion:^(BOOL success, NSError *error) {
    // Do something!
    }];

    View full-size slide

  10. HKObjectType
    HKQuantityType *stepType = [HKObjectType
    quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    NSSet *writeTypes = [NSSet setWithObject:stepType];
    NSSet *readTypes = [NSSet setWithObject:stepType];

    View full-size slide

  11. HKObjectType
    HKQuantityType *stepType = [HKObjectType
    quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    NSSet *writeTypes = [NSSet setWithObject:stepType];
    NSSet *readTypes = [NSSet setWithObject:stepType];
    [self.healthStore requestAuthorizationToShareTypes:writeTypes
    readTypes:readTypes
    completion:^(BOOL success, NSError *error) {
    // Do something!
    }];

    View full-size slide

  12. HKHealthStore
    [self.healthStore whatElseCanYouDo];

    View full-size slide

  13. HKHealthStore — Saving
    HKQuantityType *stepQuantityType = [HKQuantityType
    quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    // …

    View full-size slide

  14. HKUnit
    HKQuantityType *stepQuantityType = [HKQuantityType
    quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    HKQuantity *stepQuantity = [HKQuantity quantityWithUnit:[HKUnit countUnit]
    doubleValue:stepCount];
    // …

    View full-size slide

  15. HKQuantitySample
    HKQuantityType *stepQuantityType = [HKQuantityType
    quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    HKQuantity *stepQuantity = [HKQuantity quantityWithUnit:[HKUnit countUnit]
    doubleValue:stepCount];
    HKQuantitySample *stepSample = [HKQuantitySample quantitySampleWithType:stepQuantityType
    quantity:stepQuantity
    startDate:someStartDate
    endDate:someEndDate];

    View full-size slide

  16. HKHealthStore — Saving
    // …
    [self.healthStore saveObject:stepSample withCompletion:^(BOOL success, NSError *error) {
    // You better check that error.
    }];

    View full-size slide

  17. HKHealthStore — Querying

    View full-size slide

  18. HKHealthStore — Querying
    HKQuantityType *stepQuantityType = [HKQuantityType
    quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    //…

    View full-size slide

  19. HKHealthStore — Querying
    HKQuantityType *stepQuantityType = [HKQuantityType
    quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    NSPredicate *appPredicate = [HKSampleQuery
    predicateForObjectsFromSource:[HKSource defaultSource]];
    //…

    View full-size slide

  20. HKHealthStore — Querying
    HKQuantityType *stepQuantityType = [HKQuantityType
    quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    NSPredicate *appPredicate = [HKSampleQuery
    predicateForObjectsFromSource:[HKSource defaultSource]];
    HKSampleQuery *stepQuery = [[HKSampleQuery alloc] initWithSampleType:stepQuantityType
    predicate:appPredicate
    limit:HKObjectQueryNoLimit
    sortDescriptors:nil
    resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
    // results will be delivered as HKSamples. In this case, HKQuantitySamples.
    // You should also handle that Error!
    }];
    //…

    View full-size slide

  21. HKHealthStore — Querying
    //… stepQuery created
    [self.healthStore executeQuery:stepQuery];

    View full-size slide

  22. “Let’s build something
    cool instead.”
    — All of us, just now.

    View full-size slide

  23. With great power
    comes great
    responsibility.

    View full-size slide

  24. Thank you!
    Questions?
    Josh Johnson | Two Toasters | @jnjosh

    View full-size slide