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
Prioritizing Health: Developing a Personalized App
Search
tatetate55
March 24, 2024
1.2k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Prioritizing Health: Developing a Personalized App
tatetate55
March 24, 2024
More Decks by tatetate55
See All by tatetate55
会社休むために実装するSiri Shortcuts
tatetate55
0
290
Featured
See All Featured
Building AI with AI
inesmontani
PRO
1
1.1k
Deep Space Network (abreviated)
tonyrice
0
260
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
890
How to train your dragon (web standard)
notwaldorf
97
6.8k
How to Talk to Developers About Accessibility
jct
2
520
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Statistics for Hackers
jakevdp
799
230k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
230
So, you think you're a good person
axbom
PRO
2
2.1k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
370
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
710
Transcript
Kazuhiro Kamakura 1SJPSJUJ[JOH)FBMUI %FWFMPQJOHB1FSTPOBMJ[FE"QQ ݈߁ୈҰʂ ࣗઐ༻ͷݸਓΞϓϦ։ൃ
Kazuhiro Kamakura ח߂ 9!ZPZPLL55 LY Corporation
Do you do personal development? ࠷ۙɺݸਓ։ൃ͍ͯ͠·͔͢ʂʁ
Some people might say “I haven't done any personal development
lately.” Ҏલ͍͚ͯͨ͠Ͳɺ࠷͍ۙͯ͠ͳ͍ɾɾ
I want only one function. However, that would likely result
in rejection from the App Review. ̍ͭͷػೳ͚ͩཉ͍͚͠ͲɺͦΕͰϦδΣΫτ͞Εͦ͏ɾɾ
I have a suggestion! 💡
Why don't you develop your own app only for yourself?
ࣗͷͨΊ͚ͩͷΞϓϦ։ൃͲ͏Ͱ͠ΐ͏ʂʁ
I have created an app that only counts my steps.
🏃. →
Background ࡞ͬͨഎܠ • I know we need to walk for
our health. • But I rarely go out. 🏃..
I have an incentive Πϯηڅ༩ͷࢧڅʂ I can receive an incentive
from my company if I walk over 4,000 steps a day! https://about.yahoo.co.jp/info/blog/20210719/walking.html ※2021࣌
I have an incentive Πϯηڅ༩ͷࢧڅʂ I can receive an incentive
from my company if I walk over 4,000 steps a day! https://about.yahoo.co.jp/info/blog/20210719/walking.html I'm gonna do it! ※2021࣌
So, how did I make it?ɹ 🔨☺ ͰͲͷΑ͏ʹ࡞͍͔ͬͯͬͨʁ
How the app measures the number of steps There are
two patterns 1. CMPedometer of CoreMotion 2. HealthKit I won't use it this time ->
CMPedometer of CoreMotion 1. It can measure in real time
2. It can measure the number of steps on that device. 3. It can count steps from iOS 8.
How to use CMPedometer of CoreMotion import CoreMotion class PedometerManager
{ let pedometer = CMPedometer() // ϖυϝʔλʔ͕ར༻Մೳ͔νΣοΫ func isPedometerAvailable() -> Bool { return CMPedometer.isStepCountingAvailable() } // ϦΞϧλΠϜͰาΛऔಘ func startPedometerUpdates() { guard CMPedometer.isStepCountingAvailable() else { print("Step counting is not available.") return } pedometer.startUpdates(from: Date()) { (data, error) in guard error == nil else { print("There was an error starting pedometer updates: \(error!.localizedDescription)") return } if let pedometerData = data { print("Steps taken: \(pedometerData.numberOfSteps)") // ଞͷσʔλಉ༷ʹΞΫηεՄೳ } } } }
I can write the program with ChatGPT $IBU(15ʹฉ͚؆୯ʹ͔͚·͢ʂ Thank you!
HealthKit As you know, we can get information from HealthKit
← 📱 Same as this ->
Which one should I use? 🤔 ͰͲͪΒΛબͿ͔ʁ
HealthKit method is popular. And, it’s used for the company’s
app. So, I use Health Kit.
We can do a lot more🚶
How to use HealthKit • Add Capability • Add permission
settings for HealthKit data access to info.plist
How to use import HealthKit class HealthKitManager { let healthStore
= HKHealthStore() // HealthKitͷηοτΞοϓ init() { if HKHealthStore.isHealthDataAvailable() { let allTypes = Set([HKObjectType.quantityType(forIdentifier: .stepCount)!]) healthStore.requestAuthorization(toShare: nil, read: allTypes) { (success, error) in if !success { // ΤϥʔϋϯυϦϯά } } } } // ಛఆͷͷาΛऔಘ func getSteps(forDate date: Date, completion: @escaping (Double) -> Void) { guard let stepCountType = HKQuantityType.quantityType(forIdentifier: .stepCount) else { completion(0) return } let startOfDay = Calendar.current.startOfDay(for: date) let endOfDay = Calendar.current.date(byAdding: .day, value: 1, to: startOfDay) let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: endOfDay, options: .strictStartDate) let query = HKStatisticsQuery(quantityType: stepCountType, quantitySamplePredicate: predicate, options: .cumulativeSum) { (query, statistics, error) in var steps: Double = 0 if error != nil { // ΤϥʔϋϯυϦϯά } else if let quantity = statistics?.sumQuantity() { steps = quantity.doubleValue(for: HKUnit.count()) } DispatchQueue.main.async { completion(steps) } } healthStore.execute(query) } }
Completion of the iOS version This is what it looks
like when displayed in Swift Charts →
My App Icon ->
But, I didn’t open the iOS app ❌ ͔͠͠ɺiOS App։͔ͣɾɾɾ
So, I created a Watch widget that I can always
see.
But Xcode 14 doesn’t have HealthKit capability • Watch’s Widget
of Xcode 14 doesn’t have HealthKit capability. • Therefore, I need to transfer data from my watch or iPhone. ← Nothing…
Xcode 15 has the capability🎉 Xcode 15 gets HealthKit capability!!
I can get the same method as iOS! ΓํiOSͱಉ͡ʂ //
HealthStoreʹࠓͷาΛऔಘ͢ΔϝιουΛՃ extension HealthStore { func fetchTodaysSteps(completion: @escaping (Int) -> Void) { // HealthKitετΞ͕͋Δ͜ͱΛ֬ೝ guard let healthStore = healthStore else { return } // าλΠϓΛఆٛ͠ɺࠓͷൣғΛઃఆ let stepType = HKQuantityType.quantityType(forIdentifier: .stepCount)! let now = Date() let startOfDay = Calendar.current.startOfDay(for: now) // ΫΤϦͷൣғΛઃఆ͢ΔͨΊͷड़ޠΛ༻ let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate) // าͷ߹ܭΛऔಘ͢ΔͨΊͷΫΤϦΛ࡞ let query = HKStatisticsQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum) { _, statistics, _ in let steps = statistics?.sumQuantity()?.doubleValue(for: HKUnit.count()) ?? 0 completion(Int(steps)) } // ΫΤϦΛ࣮ߦ healthStore.execute(query) } }
Reload & Display 3FMPBE struct NumberStepsWatchExtensionEntryView : View { var
entry: Provider.Entry var body: some View { VStack { Text(String(Date().timetoString())) .font(.footnote) Text(String(entry.steps)) } .containerBackground(for: .widget) { Color.white } } } func getTimeline(in context: Context, completion: @escaping (Timeline<StepEntry>) -> ()) { let healthStore = HealthStore() healthStore.fetchTodaysSteps { stepCount in let currentDate = Date() let entries = [ StepEntry(date: currentDate, steps: stepCount) ] // ࣍ͷߋ৽࣌ࠁΛݱࡏ͔Β5ޙʹઃఆ let nextUpdateDate = Calendar.current.date(byAdding: .minute, value: 5, to: currentDate)! let timeline = Timeline(entries: entries, policy: .after(nextUpdateDate)) completion(timeline) } } %JTQMBZ
I can see it on the main screen now! I
can always see it on my Apple Watch!
As a result I can always walk over 4,000 steps
a day 🎉 ← 4000 Steps
Conclusion • It is di ffi cult to achieve exercise
with the goal of health alone. • I enjoy solving my own problems with programming. • Especially for watchOS, there are still very few in the world.
Enjoy Programming!
Thank you!