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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Asakura Shinsuke
March 16, 2018
Programming
100
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
1
Ore Keybord #1
asashin227
0
430
Build AI app with AI agent
asashin227
0
16
ReactNative入門
asashin227
0
63
アウトプットしようね
asashin227
0
32
SwiftUIでキラキラを作る
asashin227
0
83
ローカル拠点でのEMの役割
asashin227
0
280
XcodeCloud移行奮闘記
asashin227
0
130
現場主導の開発効率向上の仕組みづくり
asashin227
0
400
Other Decks in Programming
See All in Programming
Vite+ Unified Toolchain for the Web
naokihaba
0
300
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
540
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
RTSPクライアントを自作してみた話
simotin13
0
600
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
170
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.2k
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
580
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.6k
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
110
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
240
Oxlintのカスタムルールの現況
syumai
6
1.1k
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
160
Featured
See All Featured
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
240
Darren the Foodie - Storyboard
khoart
PRO
3
3.4k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
My Coaching Mixtape
mlcsv
0
150
Paper Plane (Part 1)
katiecoart
PRO
0
8.9k
We Have a Design System, Now What?
morganepeng
55
8.2k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
230
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.5k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
940
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
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