getting the most out of healthkit - apple developer€¦ · health records represent different...

192
© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. What’s new and best practices App Frameworks #WWDC16 Session 209 Getting the Most Out of HealthKit Matthew Salesi iOS Software Engineer Joefrey Kibuule iOS Software Engineer

Upload: others

Post on 23-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

What’s new and best practices

App Frameworks #WWDC16

Session 209

Getting the Most Out of HealthKit

Matthew Salesi iOS Software EngineerJoefrey Kibuule iOS Software Engineer

Page 2: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient
Page 3: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Authorization

Page 4: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Authorization

Activity Rings

Page 5: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Authorization

Activity Rings

Health Records

Page 6: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Authorization

Activity Rings

Health Records

Handling Data

Page 7: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Authorization

Page 8: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewAuthorization

Page 9: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewAuthorization

Users are in control

Page 10: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewAuthorization

Users are in controliOS mediates authorization requests

Page 11: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewAuthorization

Users are in controliOS mediates authorization requestsPermissions may change at any time

Page 12: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewAuthorization

Users are in controliOS mediates authorization requestsPermissions may change at any timeRead and write authorization are independent

Page 13: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Read/write permissionsAuthorization

Permissions Can Query? Can Save?

Read + Write Yes Yes

Read Yes No

Write Own data only Yes

None No No

Page 14: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Read/write permissionsAuthorization

Permissions Can Query? Can Save?

Read + Write Yes Yes

Read Yes No

Write Own data only Yes

None No No

Page 15: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Read/write permissionsAuthorization

Permissions Can Query? Can Save?

Read + Write Yes Yes

Read Yes No

Write Own data only Yes

None No No

Page 16: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Read/write permissionsAuthorization

Permissions Can Query? Can Save?

Read + Write Yes Yes

Read Yes No

Write Own Data Only Yes

None No No

Page 17: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Read/write permissionsAuthorization

Permissions Can Query? Can Save?

Read + Write Yes Yes

Read Yes No

Write Own Data Only Yes

None No No

Page 18: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Usage descriptionsAuthorization NEW

Page 19: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Usage descriptionsAuthorization

Apps must explain how they will use Health data

NEW

Page 20: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Usage descriptionsAuthorization

Apps must explain how they will use Health dataStatically declared in Info.plist

NEW

Page 21: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Usage descriptionsAuthorization

Apps must explain how they will use Health dataStatically declared in Info.plist• NSHealthShareUsageDescription

NEW

Page 22: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Usage descriptionsAuthorization

Apps must explain how they will use Health dataStatically declared in Info.plist• NSHealthShareUsageDescription• NSHealthUpdateUsageDescription

NEW

Page 23: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Requesting Authorization

import HealthKit

guard HKHealthStore.isHealthDataAvailable() else { return }

let healthStore = HKHealthStore()

let shareableTypes = Set([ ... ])

let readableTypes = Set([ ... ])

// Presents authorization sheet to user the first time this is called.

healthStore.requestAuthorization(toShare: shareableTypes,

read: readableTypes) { success, error in

// Handle authorization response here.

}

Page 24: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Requesting Authorization

import HealthKit

guard HKHealthStore.isHealthDataAvailable() else { return }

let healthStore = HKHealthStore()

let shareableTypes = Set([ ... ])

let readableTypes = Set([ ... ])

// Presents authorization sheet to user the first time this is called.

healthStore.requestAuthorization(toShare: shareableTypes,

read: readableTypes) { success, error in

// Handle authorization response here.

}

Page 25: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Requesting Authorization

import HealthKit

guard HKHealthStore.isHealthDataAvailable() else { return }

let healthStore = HKHealthStore()

let shareableTypes = Set([ ... ])

let readableTypes = Set([ ... ])

// Presents authorization sheet to user the first time this is called.

healthStore.requestAuthorization(toShare: shareableTypes,

read: readableTypes) { success, error in

// Handle authorization response here.

}

Page 26: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Requesting Authorization

import HealthKit

guard HKHealthStore.isHealthDataAvailable() else { return }

let healthStore = HKHealthStore()

let shareableTypes = Set([ ... ])

let readableTypes = Set([ ... ])

// Presents authorization sheet to user the first time this is called.

healthStore.requestAuthorization(toShare: shareableTypes,

read: readableTypes) { success, error in

// Handle authorization response here.

}

Page 27: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

watchOSAuthorization

Page 28: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

watchOSAuthorization

Shared with your iPhone app

Page 29: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

watchOSAuthorization

Shared with your iPhone appApproval requires interaction with iPhone

Page 30: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

watchOSAuthorization

Shared with your iPhone appApproval requires interaction with iPhone• May not be easily accessible

Page 31: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

watchOSAuthorization

Shared with your iPhone appApproval requires interaction with iPhone• May not be easily accessible• May be out of range

Page 32: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Best practicesAuthorization

Page 33: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Best practicesAuthorization

Make initial requests in sensible groupings of types

Page 34: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Best practicesAuthorization

Make initial requests in sensible groupings of typesOption: Request all your app’s types during on-boarding

Page 35: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Best practicesAuthorization

Make initial requests in sensible groupings of typesOption: Request all your app’s types during on-boardingReset authorization frequently during development

Page 36: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Best practicesAuthorization

Make initial requests in sensible groupings of typesOption: Request all your app’s types during on-boardingReset authorization frequently during developmentTest delaying/denying authorization

Page 37: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Best practicesAuthorization

Make initial requests in sensible groupings of typesOption: Request all your app’s types during on-boardingReset authorization frequently during developmentTest delaying/denying authorizationDo what’s best for the user

Page 38: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Activity Rings

Page 39: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient
Page 40: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivitySummaryActivity Rings

Page 41: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivitySummaryActivity Rings

Daily activity values

Page 42: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivitySummaryActivity Rings

Daily activity values• Move calories and goal

Page 43: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivitySummaryActivity Rings

Daily activity values• Move calories and goal• Exercise minutes and goal

Page 44: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivitySummaryActivity Rings

Daily activity values• Move calories and goal• Exercise minutes and goal• Stand hours and goal

Page 45: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivitySummaryActivity Rings

Page 46: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivitySummaryActivity Rings

Distinct type for authorization

Page 47: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivitySummaryActivity Rings

Distinct type for authorizationDaily aggregated totals

Page 48: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivitySummaryActivity Rings

Distinct type for authorizationDaily aggregated totalsDay specified as DateComponents

Page 49: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// HKActivitySummaryQuery

import HealthKit

let healthStore = HKHealthStore()

let calendar = Calendar.current()

var components = calendar.components([.era, .year, .month, .day], from:Date())

components.calendar = calendar

let predicate = Predicate(format: "%K = %@", HKPredicateKeyPathDateComponents, components)

let query = HKActivitySummaryQuery(predicate: predicate) { query, summaries, error in

guard let todayActivitySummary = summaries?.first else { return }

// Display todayActivitySummary's data.

}

healthStore.execute(query)

Page 50: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// HKActivitySummaryQuery

import HealthKit

let healthStore = HKHealthStore()

let calendar = Calendar.current()

var components = calendar.components([.era, .year, .month, .day], from:Date())

components.calendar = calendar

let predicate = Predicate(format: "%K = %@", HKPredicateKeyPathDateComponents, components)

let query = HKActivitySummaryQuery(predicate: predicate) { query, summaries, error in

guard let todayActivitySummary = summaries?.first else { return }

// Display todayActivitySummary's data.

}

healthStore.execute(query)

Page 51: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// HKActivitySummaryQuery

import HealthKit

let healthStore = HKHealthStore()

let calendar = Calendar.current()

var components = calendar.components([.era, .year, .month, .day], from:Date())

components.calendar = calendar

let predicate = Predicate(format: "%K = %@", HKPredicateKeyPathDateComponents, components)

let query = HKActivitySummaryQuery(predicate: predicate) { query, summaries, error in

guard let todayActivitySummary = summaries?.first else { return }

// Display todayActivitySummary's data.

}

healthStore.execute(query)

Page 52: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// HKActivitySummaryQuery

import HealthKit

let healthStore = HKHealthStore()

let calendar = Calendar.current()

var components = calendar.components([.era, .year, .month, .day], from:Date())

components.calendar = calendar

let predicate = Predicate(format: "%K = %@", HKPredicateKeyPathDateComponents, components)

let query = HKActivitySummaryQuery(predicate: predicate) { query, summaries, error in

guard let todayActivitySummary = summaries?.first else { return }

// Display todayActivitySummary's data.

}

healthStore.execute(query)

Page 53: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivityRingView and WKInterfaceActivityRingActivity Rings

Page 54: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivityRingView and WKInterfaceActivityRingActivity Rings

Page 55: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

HKActivityRingView and WKInterfaceActivityRingActivity Rings

Page 56: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

TipsActivity Rings

Page 57: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

TipsActivity Rings

Rings look best on black background

Page 58: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

TipsActivity Rings

Rings look best on black backgroundConstruct an HKActivitySummary to represent another user’s rings

Page 59: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

TipsActivity Rings

Rings look best on black backgroundConstruct an HKActivitySummary to represent another user’s ringsProvide era, year, month, and day in your DateComponents

Page 60: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

TipsActivity Rings

Rings look best on black backgroundConstruct an HKActivitySummary to represent another user’s ringsProvide era, year, month, and day in your DateComponents

Solutions to Common Date and Time Challenges WWDC 2013

Page 61: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

DemoAuthorization and activity rings

Page 62: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Health Records

Jeff KibuuleiOS Software Engineer

Page 63: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Current experienceHealth Records

Page 64: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Current experienceHealth Records

Page 65: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Current experienceHealth Records

Page 66: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Current experienceHealth Records

Page 67: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewHealth Records NEW

Page 68: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewHealth Records

Represent different types of patient visits

NEW

Page 69: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewHealth Records

Represent different types of patient visits

NEW

Page 70: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewHealth Records

Represent different types of patient visitsSupport international HL-7 CDA standard

NEW

Page 71: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewHealth Records

Represent different types of patient visitsSupport international HL-7 CDA standardAvailable through patient health care portals

NEW

Page 72: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewHealth Records

Represent different types of patient visitsSupport international HL-7 CDA standardAvailable through patient health care portalsImported via Safari, Mail, and your apps

NEW

Page 73: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

OverviewHealth Records

Represent different types of patient visitsSupport international HL-7 CDA standardAvailable through patient health care portalsImported via Safari, Mail, and your appsStored securely

NEW

Page 74: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

PermissionsHealth Records

Page 75: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

PermissionsHealth Records

Access granted on a per document basis

Page 76: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

PermissionsHealth Records

Access granted on a per document basisUI presented to grant access

Page 77: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

PermissionsHealth Records

Access granted on a per document basisUI presented to grant accessQueries will…

Page 78: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

PermissionsHealth Records

Access granted on a per document basisUI presented to grant accessQueries will…• Prompt UI if new documents available

Page 79: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

PermissionsHealth Records

Access granted on a per document basisUI presented to grant accessQueries will…• Prompt UI if new documents available• Return immediately if no new documents

Page 80: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

PermissionsHealth Records

Access granted on a per document basisUI presented to grant accessQueries will…• Prompt UI if new documents available• Return immediately if no new documents• Never prompt in background

Page 81: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

CreatingHealth Records

Page 82: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

CreatingHealth Records

Save raw XML document as Data into HKCDADocumentSample type

Page 83: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

CreatingHealth Records

Save raw XML document as Data into HKCDADocumentSample type Validated on sample creation

Page 84: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

CreatingHealth Records

Save raw XML document as Data into HKCDADocumentSample type Validated on sample creationTitle, patient, custodian, and author names extracted automatically

Page 85: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Creating a Health Document Using HKCDADocumentSample

let today = Date()

let documentData: Data = ... // XML from health organization server

do {

let cdaSample = try HKCDADocumentSample.init(data: documentData, start: today, end:

today, metadata: nil)

healthStore.save(cdaSample) { success, error in

// Handle saving error here...

}

} catch {

// Handle validation error creating sample here...

}

Page 86: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Creating a Health Document Using HKCDADocumentSample

let today = Date()

let documentData: Data = ... // XML from health organization server

do {

let cdaSample = try HKCDADocumentSample.init(data: documentData, start: today, end:

today, metadata: nil)

healthStore.save(cdaSample) { success, error in

// Handle saving error here...

}

} catch {

// Handle validation error creating sample here...

}

Page 87: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Creating a Health Document Using HKCDADocumentSample

let today = Date()

let documentData: Data = ... // XML from health organization server

do {

let cdaSample = try HKCDADocumentSample.init(data: documentData, start: today, end:

today, metadata: nil)

healthStore.save(cdaSample) { success, error in

// Handle saving error here...

}

} catch {

// Handle validation error creating sample here...

}

Page 88: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Creating a Health Document Using HKCDADocumentSample

let today = Date()

let documentData: Data = ... // XML from health organization server

do {

let cdaSample = try HKCDADocumentSample.init(data: documentData, start: today, end:

today, metadata: nil)

healthStore.save(cdaSample) { success, error in

// Handle saving error here...

}

} catch {

// Handle validation error creating sample here...

}

Page 89: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

QueryingHealth Records

Page 90: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

QueryingHealth Records

Existing query objects continue to work

Page 91: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

QueryingHealth Records

Existing query objects continue to workNeed to use HKDocumentQuery to fetch raw XML

Page 92: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

QueryingHealth Records

Existing query objects continue to workNeed to use HKDocumentQuery to fetch raw XMLPredicate support for searching based on extracted fields

Page 93: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

QueryingHealth Records

Existing query objects continue to workNeed to use HKDocumentQuery to fetch raw XMLPredicate support for searching based on extracted fieldsDocument updates are considered new documents

Page 94: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Querying for Health Documents Using HKDocumentQuery

guard let documentType = HKObjectType.documentType(forIdentifier: .CDA) else { return }

let cdaQuery = HKDocumentQuery(documentType: documentType, predicate: nil, limit:

HKObjectQueryNoLimit, sortDescriptors: nil, includeDocumentData: false) { query, samples,

done, error in

// Handle HKCDADocumentSamples here...

}

healthStore.execute(cdaQuery)

Page 95: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Querying for Health Documents Using HKDocumentQuery

guard let documentType = HKObjectType.documentType(forIdentifier: .CDA) else { return }

let cdaQuery = HKDocumentQuery(documentType: documentType, predicate: nil, limit:

HKObjectQueryNoLimit, sortDescriptors: nil, includeDocumentData: false) { query, samples,

done, error in

// Handle HKCDADocumentSamples here...

}

healthStore.execute(cdaQuery)

Page 96: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Querying for Health Documents Using HKDocumentQuery

guard let documentType = HKObjectType.documentType(forIdentifier: .CDA) else { return }

let cdaQuery = HKDocumentQuery(documentType: documentType, predicate: nil, limit:

HKObjectQueryNoLimit, sortDescriptors: nil, includeDocumentData: false) { query, samples,

done, error in

// Handle HKCDADocumentSamples here...

}

healthStore.execute(cdaQuery)

Page 97: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Querying for Health Documents Using HKDocumentQuery

guard let documentType = HKObjectType.documentType(forIdentifier: .CDA) else { return }

let cdaQuery = HKDocumentQuery(documentType: documentType, predicate: nil, limit:

HKObjectQueryNoLimit, sortDescriptors: nil, includeDocumentData: false) { query, samples,

done, error in

// Handle HKCDADocumentSamples here...

}

healthStore.execute(cdaQuery)

Page 98: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Querying for Health Documents Using HKDocumentQuery

guard let documentType = HKObjectType.documentType(forIdentifier: .CDA) else { return }

let cdaQuery = HKDocumentQuery(documentType: documentType, predicate: nil, limit:

HKObjectQueryNoLimit, sortDescriptors: nil, includeDocumentData: false) { query, samples,

done, error in

// Handle HKCDADocumentSamples here...

}

healthStore.execute(cdaQuery)

Page 99: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Best practicesHealth Records

Page 100: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Best practicesHealth Records

Check validation errors

Page 101: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Best practicesHealth Records

Check validation errorsVerify with Health app

Page 102: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Best practicesHealth Records

Check validation errorsVerify with Health appRequest raw XML document only when needed

Page 103: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

http://www.hl7.org/implement/standards/product_brief.cfm?product_id=7

HL-7 CDA Standard

Page 104: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Handling Data

Page 105: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Handling Data

Page 106: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Handling Data

YourApp

App#3

App#2

Page 107: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Handling Data

YourApp

App#3

App#2

Page 108: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Handling Data

YourApp

Your Cloud

App#3

App#2

Page 109: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Handling Data

YourApp

Your Cloud

App#3

App #2Cloud

App#2

Page 110: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Handling Data

Page 111: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Handling Data

Syncing data

Page 112: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Handling Data

Syncing dataTracking changed data

Page 113: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Handling Data

Syncing dataTracking changed dataMigrating data

Page 114: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Syncing dataHandling Data

Page 115: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Syncing dataHandling Data

Use HKAnchoredObjectQuery

Page 116: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Syncing dataHandling Data

Use HKAnchoredObjectQueryAnchors let you pick up where you last left off

Page 117: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Syncing dataHandling Data

Use HKAnchoredObjectQueryAnchors let you pick up where you last left offOne query per sample type

Page 118: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Syncing dataHandling Data

Use HKAnchoredObjectQueryAnchors let you pick up where you last left offOne query per sample typeUse an update handler

Page 119: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Background updatesHandling Data

Page 120: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Background updatesHandling Data

Setup

Execution

Page 121: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Background updatesHandling Data

Register for Background Updates1

Setup

Execution

Page 122: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Background updatesHandling Data

Register for Background Updates

OpenHKObserverQuery

1

2Setup

Execution

Page 123: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Background updatesHandling Data

Register for Background Updates

OpenHKObserverQuery

HKObserverQuery callback;Execute

HKAnchoredObjectQuery

1

2

3

Setup

Execution

Page 124: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Background updatesHandling Data

Register for Background Updates

OpenHKObserverQuery

HKObserverQuery callback;Execute

HKAnchoredObjectQuery

Call HKObserverQueryCompletion Handler

1

2

3

4

Setup

Execution

Page 125: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Background updatesHandling Data

Register for Background Updates

OpenHKObserverQuery

HKObserverQuery callback;Execute

HKAnchoredObjectQuery

Call HKObserverQueryCompletion Handler

1

2

3

4

Setup

Execution

Page 126: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 1. Register for background updates

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:

[NSObject: AnyObject]?) -> Bool {

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else {

return true

}

healthStore.enableBackgroundDelivery(for: stepsType, frequency: .daily) {

success, error in

// Handle enabling background delivery error here...

}

return true

}

Page 127: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 1. Register for background updates

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:

[NSObject: AnyObject]?) -> Bool {

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else {

return true

}

healthStore.enableBackgroundDelivery(for: stepsType, frequency: .daily) {

success, error in

// Handle enabling background delivery error here...

}

return true

}

Page 128: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 1. Register for background updates

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:

[NSObject: AnyObject]?) -> Bool {

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else {

return true

}

healthStore.enableBackgroundDelivery(for: stepsType, frequency: .daily) {

success, error in

// Handle enabling background delivery error here...

}

return true

}

Page 129: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 1. Register for background updates

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:

[NSObject: AnyObject]?) -> Bool {

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else {

return true

}

healthStore.enableBackgroundDelivery(for: stepsType, frequency: .daily) {

success, error in

// Handle enabling background delivery error here...

}

return true

}

Page 130: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 2. Open observer query

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else { return }

let query = HKObserverQuery(sampleType: stepsType, predicate: nil) {

query, completionHandler, error in

self.updateSteps() {

completionHandler()

}

}

healthStore.execute(query)

Page 131: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 2. Open observer query

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else { return }

let query = HKObserverQuery(sampleType: stepsType, predicate: nil) {

query, completionHandler, error in

self.updateSteps() {

completionHandler()

}

}

healthStore.execute(query)

Page 132: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 2. Open observer query

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else { return }

let query = HKObserverQuery(sampleType: stepsType, predicate: nil) {

query, completionHandler, error in

self.updateSteps() {

completionHandler()

}

}

healthStore.execute(query)

Page 133: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 2. Open observer query

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else { return }

let query = HKObserverQuery(sampleType: stepsType, predicate: nil) {

query, completionHandler, error in

self.updateSteps() {

completionHandler()

}

}

healthStore.execute(query)

Page 134: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 3. Execute anchored objected query

func updateSteps(completionHandler: () -> Void) {

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else {return}

let anchoredQuery = HKAnchoredObjectQuery(type: stepsType, predicate: nil, anchor:

self.anchor, limit: Int(HKObjectQueryNoLimit)) { [unowned self] query, newSamples,

deletedSamples, newAnchor, error -> Void in

self.handleSteps(new: newSamples, deleted: deletedSamples)

self.update(anchor: newAnchor)

completionHandler()

}

healthStore.execute(anchoredQuery)

}

Page 135: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 3. Execute anchored objected query

func updateSteps(completionHandler: () -> Void) {

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else {return}

let anchoredQuery = HKAnchoredObjectQuery(type: stepsType, predicate: nil, anchor:

self.anchor, limit: Int(HKObjectQueryNoLimit)) { [unowned self] query, newSamples,

deletedSamples, newAnchor, error -> Void in

self.handleSteps(new: newSamples, deleted: deletedSamples)

self.update(anchor: newAnchor)

completionHandler()

}

healthStore.execute(anchoredQuery)

}

Page 136: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 3. Execute anchored objected query

func updateSteps(completionHandler: () -> Void) {

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else {return}

let anchoredQuery = HKAnchoredObjectQuery(type: stepsType, predicate: nil, anchor:

self.anchor, limit: Int(HKObjectQueryNoLimit)) { [unowned self] query, newSamples,

deletedSamples, newAnchor, error -> Void in

self.handleSteps(new: newSamples, deleted: deletedSamples)

self.update(anchor: newAnchor)

completionHandler()

}

healthStore.execute(anchoredQuery)

}

Page 137: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 3. Execute anchored objected query

func updateSteps(completionHandler: () -> Void) {

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else {return}

let anchoredQuery = HKAnchoredObjectQuery(type: stepsType, predicate: nil, anchor:

self.anchor, limit: Int(HKObjectQueryNoLimit)) { [unowned self] query, newSamples,

deletedSamples, newAnchor, error -> Void in

self.handleSteps(new: newSamples, deleted: deletedSamples)

self.update(anchor: newAnchor)

completionHandler()

}

healthStore.execute(anchoredQuery)

}

Page 138: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 3. Execute anchored objected query

func updateSteps(completionHandler: () -> Void) {

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else {return}

let anchoredQuery = HKAnchoredObjectQuery(type: stepsType, predicate: nil, anchor:

self.anchor, limit: Int(HKObjectQueryNoLimit)) { [unowned self] query, newSamples,

deletedSamples, newAnchor, error -> Void in

self.handleSteps(new: newSamples, deleted: deletedSamples)

self.update(anchor: newAnchor)

completionHandler()

}

healthStore.execute(anchoredQuery)

}

Page 139: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 3. Execute anchored objected query

func updateSteps(completionHandler: () -> Void) {

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else {return}

let anchoredQuery = HKAnchoredObjectQuery(type: stepsType, predicate: nil, anchor:

self.anchor, limit: Int(HKObjectQueryNoLimit)) { [unowned self] query, newSamples,

deletedSamples, newAnchor, error -> Void in

self.handleSteps(new: newSamples, deleted: deletedSamples)

self.update(anchor: newAnchor)

completionHandler()

}

healthStore.execute(anchoredQuery)

}

Page 140: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 4. Call observer query completion handler

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else { return }

let query = HKObserverQuery(sampleType: stepsType, predicate: nil) {

query, completionHandler, error in

self.updateSteps() {

completionHandler()

}

}

healthStore.execute(query)

Page 141: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

// Syncing Data

// 4. Call observer query completion handler

guard let stepsType = HKObjectType.quantityType(forIdentifier: .stepCount) else { return }

let query = HKObserverQuery(sampleType: stepsType, predicate: nil) {

query, completionHandler, error in

self.updateSteps() {

completionHandler()

}

}

healthStore.execute(query)

Page 142: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Tracking Changed Data

Page 143: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Tracking Changed Data

Use UUIDs to keep track of unique HKObjects

Page 144: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Tracking Changed Data

Use UUIDs to keep track of unique HKObjectsRecord UUIDs of HKObjects in your own data store

Page 145: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Tracking Changed Data

Use UUIDs to keep track of unique HKObjectsRecord UUIDs of HKObjects in your own data storeWhen samples are deleted, remove data corresponding to those UUIDs

Page 146: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Tracking Changed Data

Use UUIDs to keep track of unique HKObjectsRecord UUIDs of HKObjects in your own data storeWhen samples are deleted, remove data corresponding to those UUIDsEnsure sync doesn’t re-add already deleted samples

Page 147: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Potential problemsDuplication

Page 148: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Potential problemsDuplication

Pre-populating data during on-boarding saves time

Page 149: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Potential problemsDuplication

Pre-populating data during on-boarding saves timeUsers can verify/change data

Page 150: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Potential problemsDuplication

Pre-populating data during on-boarding saves timeUsers can verify/change dataProblem: Saving unchanged values

Page 151: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Potential problemsDuplication

Pre-populating data during on-boarding saves timeUsers can verify/change dataProblem: Saving unchanged values• Only save data again if this is the

user’s intent

Page 152: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Potential problemsDuplication

YourApp

App#2

Page 153: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Potential problemsDuplication

Problem: Ingesting information from another app and HealthKit Your

AppApp#2

Page 154: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Potential problemsDuplication

Problem: Ingesting information from another app and HealthKit• Pick only one source most appropriate

to your app

YourApp

App#2

Page 155: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Potential problemsDuplication

Problem: Ingesting information from another app and HealthKit• Pick only one source most appropriate

to your app• Do not save data on another app’s behalf

YourApp

App#2

Page 156: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Potential problemsDuplication

Problem: Ingesting information from another app and HealthKit• Pick only one source most appropriate

to your app• Do not save data on another app’s behalf• Write only your own data once

YourApp

App#2

Page 157: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

ExceptionsDuplication

Page 158: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

ExceptionsDuplication

Sometimes duplication is intentional

Page 159: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

ExceptionsDuplication

Sometimes duplication is intentional• Data from multiple sources

Page 160: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

ExceptionsDuplication

Sometimes duplication is intentional• Data from multiple sources

HKStatisticsQuery and HKStatisticsCollectionQuery automatically de-duplicate data

Page 161: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

ExceptionsDuplication

Sometimes duplication is intentional• Data from multiple sources

HKStatisticsQuery and HKStatisticsCollectionQuery automatically de-duplicate data Order of preferred data sources can change in Health app

Page 162: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Migrating Data

Page 163: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Migrating Data

Page 164: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Migrating Data

98° C

Page 165: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Migrating Data

Find Old Samples

98° C

Page 166: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Migrating Data

Find Old Samples

Write New Samples

98° F98° C

Page 167: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Migrating Data

Find Old Samples

Write New Samples

Delete Old Samples

98° F

Page 168: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Flow between iPhone and Apple WatchHandling Data NEW

Page 169: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Flow between iPhone and Apple WatchHandling Data

Recent samples from iPhone are periodically synced to Apple Watch

Time

iOS 9.3

iPhone Apple Watch

NEW

Page 170: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Flow between iPhone and Apple WatchHandling Data

Recent samples from iPhone are periodically synced to Apple WatchSamples on Apple Watch are pruned by end date

Time

iOS 9.3

iPhone Apple Watch

NEW

Page 171: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Flow between iPhone and Apple WatchHandling Data

Recent samples from iPhone are periodically synced to Apple WatchSamples on Apple Watch are pruned by end dateSave samples with endDate after HKHealthStore’s earliestPermittedSampleDate

Time

iOS 9.3

iPhone Apple Watch

NEW

Page 172: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Flow between iPhone and Apple WatchHandling Data

Recent samples from iPhone are periodically synced to Apple WatchSamples on Apple Watch are pruned by end dateSave samples with endDate after HKHealthStore’s earliestPermittedSampleDate

Save samples on iPhone or Apple Watch, not both

Time

iOS 9.3

iPhone Apple Watch

NEW

Page 173: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Wheelchair Support

Page 174: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient
Page 175: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Wheelchair Support NEW

Page 176: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Wheelchair Support

New characteristic data type

NEW

Page 177: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Wheelchair Support

New characteristic data typeNew quantity types

NEW

Page 178: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Wheelchair Support

New characteristic data typeNew quantity typesNew workout activity types

NEW

Page 179: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

DetailsWheelchair Support NEW

Page 180: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

DetailsWheelchair Support

Steps → Push count

NEW

Page 181: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

DetailsWheelchair Support

Steps → Push count

Stand hours → Roll hours

NEW

Page 182: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

DetailsWheelchair Support

Steps → Push count

Stand hours → Roll hoursWheelchair distance only recorded during workouts

NEW

Page 183: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

DetailsWheelchair Support

Steps → Push count

Stand hours → Roll hoursWheelchair distance only recorded during workoutsWheelchair use status can change over time

NEW

Page 184: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Summary

Page 185: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Summary

Pay attention to the authorization user experience

Page 186: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Summary

Pay attention to the authorization user experienceIncorporate Apple’s activity rings into your app

Page 187: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Summary

Pay attention to the authorization user experienceIncorporate Apple’s activity rings into your appTake care when handling and synchronizing HealthKit data

Page 188: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Summary

Pay attention to the authorization user experienceIncorporate Apple’s activity rings into your appTake care when handling and synchronizing HealthKit dataMake your experience accessible to wheelchair users

Page 189: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

More Information

https://developer.apple.com/wwdc16/209

Page 190: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Related Sessions

Health and Fitness with Core Motion Nob Hill Thursday 3:00PM

What’s New in ResearchKit Nob Hill Friday 10:00AM

Building Great Workout Apps Pacific Heights Friday 11:00AM

Getting Started with CareKit Pacific Heights Friday 3:00PM

Introducing HealthKit WWDC 2014

Designing Accessories for iOS and OS X WWDC 2014

What’s New in HealthKit WWDC 2015

Page 191: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient

Labs

HealthKit Lab Frameworks Lab A Wednesday 10:00AM

HealthKit Lab Frameworks Lab A Thursday 9:00AM

ResearchKit and CareKit Lab Fort Mason Friday 10:30AM

Core Motion Lab Frameworks Lab D Friday 12:30PM

ResearchKit and CareKit Lab Fort Mason Friday 3:30PM

Page 192: Getting the Most Out of HealthKit - Apple Developer€¦ · Health Records Represent different types of patient visits Support international HL-7 CDA standard Available through patient