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
8
Ore Keybord #1
asashin227
0
1.5k
Build AI app with AI agent
asashin227
0
17
ReactNative入門
asashin227
0
71
アウトプットしようね
asashin227
0
34
SwiftUIでキラキラを作る
asashin227
0
89
ローカル拠点でのEMの役割
asashin227
0
290
XcodeCloud移行奮闘記
asashin227
0
130
現場主導の開発効率向上の仕組みづくり
asashin227
0
400
Other Decks in Programming
See All in Programming
改善しないと、タスクが回らない。 “てんこ盛りポジション” を引き継いだ情シスの、入社3ヶ月の業務改善録
krm963
0
250
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
580
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
140
作るコストが小さくなった時代 幸せに働くために改めて考えたいこと 〜エンジニアとして価値を出し続けるために注視している二分野〜
yuppeeng
0
180
仕様駆動開発の消費期限
watany
7
2.9k
Android CLI
fornewid
0
210
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
550
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
660
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.5k
Go言語とトイモデルで学ぶTransformerの気持ち / fukuokago23-transformer
monochromegane
0
160
霧の中の代数的エフェクト
funnyycat
1
460
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
550
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
What does AI have to do with Human Rights?
axbom
PRO
1
2.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
240
Raft: Consensus for Rubyists
vanstee
141
7.6k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
480
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
430
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
190
Typedesign – Prime Four
hannesfritz
42
3.1k
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