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

HealthKitを触ってみよう

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

 HealthKitを触ってみよう

Avatar for Asakura Shinsuke

Asakura Shinsuke

March 16, 2018
Tweet

More Decks by Asakura Shinsuke

Other Decks in Programming

Transcript

  1. 1. HealthKit΁ͷΞΫηεڐ Մ // ॻ͖ࠐΈλΠϓ let typesToWrite: Set<HKSampleType> = [HKQuantityType.quantityType(forIdentifier:

    .stepCount)!] // ಡΈࠐΈλΠϓ let typesToRead: Set<HKObjectType> = [HKQuantityType.quantityType(forIdentifier: .stepCount)!] let healthStore: HKHealthStore? = { // HealthKit͕༗ޮͳσόΠε͔ HKHealthStore.isHealthDataAvailable() ? HKHealthStore() : nil }() /// HealthKit΁ͷΞΫηεΛٻΊΔ healthStore.requestAuthorization(toShare: typesToShare, read: typesToRead) { success, error in // do something }
  2. 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) }
  3. ݁Ռ 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 ---
  4. ߹ܭ஋ΛͱΔ let sum = results.reduce(0) { if let q =

    $1 as? HKQuantitySample { return $0 + Int(q.quantity.doubleValue(for: unit)) } return $0 } print("߹ܭ஋: \(sum)") ߹ܭ஋: 21156
  5. !? 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
  6. ޡࢉ ! શͯͷσόΠε͔ΒͷσʔλΛऔಘ͠ ͯ͠·͍ɺॏෳ͢Δ͕࣌ؒଘࡏ͢Δ 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 ---
  7. ౷ܭσʔλΛऔಘ͢Δ 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) }
  8. 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)") } }) }