A Tiny Success!

The tutorial I had originally been following was taken down from YouTube so I had to a new one that would guide me through the process of accessing a user’s data from HealthKit. I found a tutorial about counting steps using HealthKit that took me just a few hours to complete.

One of the most important things I learned from this tutorial was how to generate a query using HKStatisticsCollection. The query takes a quantity type (this will be the type of metric I request from the user), a predicate (a time range to grab data from…this tutorial looks at the last seven days from the current date), and a few other things. I ideally want to grab data consistently so I’ll be looking into how to actively grab data (probably by having an app that can run in the background?).

The way the code comes together is pretty neat. Essentially there’s a struct “Step” that contains a UUID, step count, and date. In the ContentView file (what the user will see) we create a Step array called “steps” that saves Step instances when initialized. The “body” of the watch screen then gets a steps List and displays them in a VStack.


By connecting my phone to my laptop I was able to run the application on my watch. In order to access step count data I again had to request authorization from the user.

Opens to another screen asking what data to share (in this case only “steps” are requested, but the user has the ability to select all available data options or particular ones.
Doesn’t show much but we can see dates and step counts!
In the video above we can see that on the current date (February 8) I had 4,848 steps. I compared this to the steps I saw on my phone’s health app (along with the other dates) and found them to be the same…nice!

Leave a Reply

Your email address will not be published.

css.php