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 Slide

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

    View Slide

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

    View Slide

  4. “Hello Computer”
    — Scotty

    View Slide

  5. View Slide

  6. View Slide

  7. View Slide

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

    View Slide

  9. View Slide

  10. The HealthKit Crash Course™

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  15. 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 Slide

  16. View Slide

  17. View Slide

  18. HKHealthStore
    [self.healthStore whatElseCanYouDo];

    View Slide

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

    View Slide

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

    View Slide

  21. 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 Slide

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

    View Slide

  23. View Slide

  24. HKHealthStore — Querying

    View Slide

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

    View Slide

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

    View Slide

  27. 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 Slide

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

    View Slide

  29. View Slide

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

    View Slide

  31. View Slide

  32. View Slide

  33. Demo

    View Slide

  34. With great power
    comes great
    responsibility.

    View Slide

  35. View Slide

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

    View Slide

  37. View Slide