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
4
Ore Keybord #1
asashin227
0
1.1k
Build AI app with AI agent
asashin227
0
16
ReactNative入門
asashin227
0
63
アウトプットしようね
asashin227
0
32
SwiftUIでキラキラを作る
asashin227
0
84
ローカル拠点でのEMの役割
asashin227
0
280
XcodeCloud移行奮闘記
asashin227
0
130
現場主導の開発効率向上の仕組みづくり
asashin227
0
400
Other Decks in Programming
See All in Programming
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
270
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
180
スマートグラスで並列バイブコーディング
hyshu
0
260
はてなアカウント基盤 State of the Union
cockscomb
1
970
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
870
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
8.7k
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
150
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
170
act1-costs.pdf
sumedhbala
0
120
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
310
OSもどきOS
arkw
0
600
Inside Stream API
skrb
1
800
Featured
See All Featured
Six Lessons from altMBA
skipperchong
29
4.3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
The Limits of Empathy - UXLibs8
cassininazir
1
370
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
340
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.7k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
250
Making Projects Easy
brettharned
120
6.7k
Prompt Engineering for Job Search
mfonobong
0
350
WENDY [Excerpt]
tessaabrams
11
38k
My Coaching Mixtape
mlcsv
0
160
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