Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
HealthKitを触ってみよう
Search
Asakura Shinsuke
March 16, 2018
Programming
110
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
HealthKitを触ってみよう
Asakura Shinsuke
March 16, 2018
More Decks by Asakura Shinsuke
See All by Asakura Shinsuke
Creating an English conversation app using the Foundation Models framework
asashin227
0
7
Ore Keybord #1
asashin227
0
1.5k
Build AI app with AI agent
asashin227
0
17
ReactNative入門
asashin227
0
66
アウトプットしようね
asashin227
0
32
SwiftUIでキラキラを作る
asashin227
0
88
ローカル拠点でのEMの役割
asashin227
0
290
XcodeCloud移行奮闘記
asashin227
0
130
現場主導の開発効率向上の仕組みづくり
asashin227
0
400
Other Decks in Programming
See All in Programming
はてなアカウント基盤 State of the Union
cockscomb
1
1.3k
The Bowling Game- From Imperative to Functional Programming - Part 1
philipschwarz
PRO
0
340
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
270
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
150
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
130
PHPだって関数型したい 〜できること、できないこと〜 / fp-in-php
jsoizo
1
240
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
110
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
390
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
510
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
500
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
AIが無かった頃の素敵な出会いの話
codmoninc
1
160
Featured
See All Featured
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
610
Embracing the Ebb and Flow
colly
88
5.1k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
How to Ace a Technical Interview
jacobian
281
24k
Agile that works and the tools we love
rasmusluckow
331
22k
Testing 201, or: Great Expectations
jmmastey
46
8.2k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
970
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
470
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
310
Discover your Explorer Soul
emna__ayadi
2
1.2k
A Soul's Torment
seathinner
6
3.1k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
450
Transcript
HealthKitΛ৮ͬͯΈ· ͠ΐ͏
ࣗݾհ ே৻྄ • Twitter: @asashin227 • Github: @asashin227 iOS ΤϯδχΞ
(ג)Prevent
HealthKit • iOS 8͔ΒՃ • Ϣʔβͷ݈߁ঢ়ଶΛऩू͠ɺApp͔ΒಡΈऔΓɺAppఏڙ͢Δ
ରσʔλ • iPhone • า • ΥʔΩϯάɺϥϯχϯάͷڑ • ొͬͨ֊ஈ •
Watch • า • ΥʔΩϯάɺϥϯχϯάͷڑ
͍ํ 1. HealthKitͷΞΫηεڐՄ 2. σʔλͷऔಘ 3. σʔλͷॻ͖ࠐΈ
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 }
None
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) }
2. σʔλͷऔಘ let now = Date() self.executeSampleQuery(type: HKSampleType.quantityType(forIdentifier: .stepCount)!, unit:
HKUnit.count(), startDate: now.add(day: -1), endDate: now)
݁Ռ 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 ---
߹ܭΛͱΔ let sum = results.reduce(0) { if let q =
$1 as? HKQuantitySample { return $0 + Int(q.quantity.doubleValue(for: unit)) } return $0 } print("߹ܭ: \(sum)") ߹ܭ: 21156
!? 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
ޡࢉ ! શͯͷσόΠε͔ΒͷσʔλΛऔಘ͠ ͯ͠·͍ɺॏෳ͢Δ͕࣌ؒଘࡏ͢Δ 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 ---
౷ܭσʔλΛऔಘ͢Δ 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) }
2. ౷ܭσʔλΛऔಘ͢Δ let now = Date() self.executeCollectionQuery(type: HKSampleType.quantityType(forIdentifier: .stepCount)!, unit:
HKUnit.count(), startDate: now.add(day: -1), endDate: now)
݁Ռ startDate: 2018-03-10 11:53:32 +0000 endDate: 2018-03-11 11:53:32 +0000 value:
11036.9798997499
None
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)") } }) }
3.σʔλͷॻ͖ࠐΈ let now = Date() self.saveHealthKit(doubleValue: 5, type: HKQuantityType.quantityType(forIdentifier: .stepCount)!,
unit: HKUnit.count(), startDate: now.add(minute: -5), endDate: now)
None
αϯϓϧίʔυ asashin227/HealthKitSample https:/ /github.com/asashin227/HealthKitSample
ࢀߟ HealthKit | Apple Developer Documentation https:/ /developer.apple.com/documentation/healthkit