Slide 1

Slide 1 text

HealthKitΛ৮ͬͯΈ· ͠ΐ͏

Slide 2

Slide 2 text

ࣗݾ঺հ ே૔৻྄ • Twitter: @asashin227 • Github: @asashin227 iOS ΤϯδχΞ (ג)Prevent

Slide 3

Slide 3 text

HealthKit • iOS 8͔Β௥Ճ • Ϣʔβͷ݈߁ঢ়ଶΛऩू͠ɺApp͔ΒಡΈऔΓɺApp΁ఏڙ͢Δ

Slide 4

Slide 4 text

ର৅σʔλ • iPhone • า਺ • ΢ΥʔΩϯάɺϥϯχϯάͷڑ཭ • ొͬͨ֊ஈ • Watch • า਺ • ΢ΥʔΩϯάɺϥϯχϯάͷڑ཭

Slide 5

Slide 5 text

࢖͍ํ 1. HealthKit΁ͷΞΫηεڐՄ 2. σʔλͷऔಘ 3. σʔλͷॻ͖ࠐΈ

Slide 6

Slide 6 text

1. HealthKit΁ͷΞΫηεڐ Մ // ॻ͖ࠐΈλΠϓ let typesToWrite: Set = [HKQuantityType.quantityType(forIdentifier: .stepCount)!] // ಡΈࠐΈλΠϓ let typesToRead: Set = [HKQuantityType.quantityType(forIdentifier: .stepCount)!] let healthStore: HKHealthStore? = { // HealthKit͕༗ޮͳσόΠε͔ HKHealthStore.isHealthDataAvailable() ? HKHealthStore() : nil }() /// HealthKit΁ͷΞΫηεΛٻΊΔ healthStore.requestAuthorization(toShare: typesToShare, read: typesToRead) { success, error in // do something }

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

2. σʔλͷऔಘ func executeSampleQuery(type: HKSampleType, unit: HKUnit, startDate: Date, endDate: Date) { let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [.strictStartDate, .strictEndDate]) let query = HKSampleQuery(sampleType: type, predicate: predicate, limit: 0, sortDescriptors: nil) { _, results, _ in guard let results = results else { return } results.forEach { result in if let q = result as? HKQuantitySample { print("startDate: \(q.startDate)") print("endDate: \(q.endDate)") if let device = q.device { print("device: \(device.name!)") } print("value: \(q.quantity.doubleValue(for: unit))") print("---") } } } healthStore!.execute(query) }

Slide 9

Slide 9 text

2. σʔλͷऔಘ let now = Date() self.executeSampleQuery(type: HKSampleType.quantityType(forIdentifier: .stepCount)!, unit: HKUnit.count(), startDate: now.add(day: -1), endDate: now)

Slide 10

Slide 10 text

݁Ռ startDate: 2018-03-11 11:00:20 +0000 endDate: 2018-03-11 11:06:27 +0000 device: iPhone value: 11.0 --- startDate: 2018-03-11 11:01:19 +0000 endDate: 2018-03-11 11:07:23 +0000 device: Apple Watch value: 37.0 --- startDate: 2018-03-11 11:06:27 +0000 endDate: 2018-03-11 11:14:30 +0000 device: iPhone value: 8.0 --- startDate: 2018-03-11 11:15:01 +0000 endDate: 2018-03-11 11:15:40 +0000 device: Apple Watch value: 13.0 ---

Slide 11

Slide 11 text

߹ܭ஋ΛͱΔ let sum = results.reduce(0) { if let q = $1 as? HKQuantitySample { return $0 + Int(q.quantity.doubleValue(for: unit)) } return $0 } print("߹ܭ஋: \(sum)") ߹ܭ஋: 21156

Slide 12

Slide 12 text

!? HealthKareΞϓϦͱ஋͕ҧ͏ startDate: 2018-03-11 11:06:27 +0000 endDate: 2018-03-11 11:14:30 +0000 device: iPhone value: 8.0 --- startDate: 2018-03-11 11:15:01 +0000 endDate: 2018-03-11 11:15:40 +0000 device: Apple Watch value: 13.0 --- ߹ܭ஋: 21156

Slide 13

Slide 13 text

ޡࢉ ! શͯͷσόΠε͔ΒͷσʔλΛऔಘ͠ ͯ͠·͍ɺॏෳ͢Δ͕࣌ؒଘࡏ͢Δ startDate: 2018-03-11 11:00:20 +0000 endDate: 2018-03-11 11:06:27 +0000 device: iPhone value: 11.0 --- startDate: 2018-03-11 11:01:19 +0000 endDate: 2018-03-11 11:07:23 +0000 device: Apple Watch value: 37.0 ---

Slide 14

Slide 14 text

౷ܭσʔλΛऔಘ͢Δ func executeCollectionQuery(type: HKQuantityType, unit: HKUnit, startDate: Date, endDate: Date) { var components = DateComponents() components.day = 1 let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [.strictStartDate, .strictEndDate]) let collectionQuerty = HKStatisticsCollectionQuery(quantityType: type, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: startDate, intervalComponents: components) collectionQuerty.initialResultsHandler = { _, results, _ in guard let results = results else { return } results.enumerateStatistics(from: startDate, to: endDate) { result, _ in if let q = result.sumQuantity() { print("startDate: \(result.startDate)") print("endDate: \(result.endDate)") print("value: \(q.doubleValue(for: unit))") } } } healthStore!.execute(collectionQuerty) }

Slide 15

Slide 15 text

2. ౷ܭσʔλΛऔಘ͢Δ let now = Date() self.executeCollectionQuery(type: HKSampleType.quantityType(forIdentifier: .stepCount)!, unit: HKUnit.count(), startDate: now.add(day: -1), endDate: now)

Slide 16

Slide 16 text

݁Ռ startDate: 2018-03-10 11:53:32 +0000 endDate: 2018-03-11 11:53:32 +0000 value: 11036.9798997499

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

3. σʔλͷॻ͖ࠐΈ func saveHealthKit(doubleValue: Double, type: HKQuantityType, unit: HKUnit, startDate: Date, endDate: Date) { let quantity = HKQuantity(unit: unit, doubleValue: doubleValue) let obj = HKQuantitySample(type: type, quantity: quantity, start: startDate, end: endDate) healthStore!.save(obj, withCompletion: { success, error in print("result: \(success)") if let error = error { print("error: \(error.localizedDescription)") } }) }

Slide 19

Slide 19 text

3.σʔλͷॻ͖ࠐΈ let now = Date() self.saveHealthKit(doubleValue: 5, type: HKQuantityType.quantityType(forIdentifier: .stepCount)!, unit: HKUnit.count(), startDate: now.add(minute: -5), endDate: now)

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

αϯϓϧίʔυ asashin227/HealthKitSample https:/ /github.com/asashin227/HealthKitSample

Slide 22

Slide 22 text

ࢀߟ HealthKit | Apple Developer Documentation https:/ /developer.apple.com/documentation/healthkit