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
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
420
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
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
740
AI時代のUIはどこへ行く?その2!
yusukebe
21
7.1k
3Dシーンの圧縮
fadis
1
770
ふつうのFeature Flag実践入門
irof
7
3.8k
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
Signal Forms: Beyond the Basics @ngBaguette 2026 in Paris
manfredsteyer
PRO
0
250
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
540
運用エージェントは "作る" から "育てる" へ - 記憶と自己進化の3層設計パターン / self-evolving-agents-three-layer-agent-design
gawa
12
3.7k
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
10
4k
CSC307 Lecture 17
javiergs
PRO
0
320
Oxlintのカスタムルールの現況
syumai
6
1.1k
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
330
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
940
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Technical Leadership for Architectural Decision Making
baasie
3
410
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
730
The Mindset for Success: Future Career Progression
greggifford
PRO
0
360
New Earth Scene 8
popppiees
3
2.3k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.2k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
160
The browser strikes back
jonoalderson
0
1.2k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
230
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
840
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.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