How to Use Apple HealthKit Data in an App | BitBakery Software

How to Use Apple HealthKit Data in an App

May 7th, 2024 by Annaliese Schropp

Our smartphones know much more about us than what we’re scrolling through on Instagram. The sensors in iPhone smartphones can measure everything from how many steps we’ve taken in a day to the length of our stride. If you wear an Apple Watch, your phone knows even more, including your heartbeat, respiration rate, and even how much sleep you get each evening.

Developers can use this data in their apps with Apple HealthKit. This data connection allows developers to read health data collected by Apple devices and write data to the device’s HealthKit database. 

Health app Strava uses HealthKit to read and write workout data to the HealthKit database. If you record a run using Strava, the app will write that workout data—pace, distance, splits—to the HealthKit database. Or, if you complete a workout in Apple Fitness, the workout data can be sent to Strava to include in its profile of your workouts.  

HealthKit can provide deep insights and deliver engaging user experiences. But like any API or data source, you must consider user consent management, data accuracy, and framework limitations before writing that first line of code.

In this post, we’ll examine the key features developers should know about and share some of what we learned from integrating HealthKit into an app for one of our clients.

What should developers know about HealthKit?

Apple HealthKit was launched at the Apple Worldwide Developer Conference in June 2014, along with the Health app on iOS. Each progressive release of iOS and watchOS has added to the data types HealthKit can track, including blood oxygen level and fertility tracking. As with any Apple product, the company has touted HealthKit's security and privacy features as a critical advantage over its competitors.

Here are three things you should know about HealthKit:

  1. HealthKit prioritizes user privacy. Developers must obtain explicit user consent before accessing or writing any health data. The consent is very granular, with the user needing to give permission for each HealthKit data type the app will access or write.

  2. HealthKit data categories. HealthKit offers a wide range of data categories, including sleep, activity, vitals, nutrition, and medication tracking. Some of these categories require the user to enter data manually, such as medication and nutrition. Categories like steps, heart rate, and environmental sound levels are populated using iPhone or Apple Watch sensors.

  3. Accessing and writing data. Devote some time to the documentation for HKHealthStore. It acts as the central hub for interacting with HealthKit data, allowing you to request user permission to access and write various health data types, manage saved samples, and initiate queries for specific information.


Integrating HealthKit into a health and wellness app

We supported Women’s Brain Health Initiative by developing and launching BrainFit, a free habit-tracking app for iOS and Android devices. The app aims to help people build habits to improve their brain health and reduce their risks for dementia and Alzheimer’s Disease.

After reviewing the app’s usage and conducting user interviews, we decided that the time was right to introduce a HealthKit integration. Android has a similar health feature called Health Connect, but based on app installs and usage, we decided to integrate HealthKit first.

After a few weeks of development and testing (and more testing), we released a new version of the app with the HealthKit integration. For this integration, we used HealthKit to access health data from the HealthKit database and automatically update the user's tracked habits. 

The user can see which habits are HealthKit-enabled when viewing the habit list. Once selected, they can give their permission to HealthKit and then see the results as they progress.

In this release of BrainFit, users can add these HealthKit-connected habits:

What did we learn about HealthKit?

We focused on accessing data from HealthKit for this release. We’re not writing any data to the HealthKit database, which made the integration process more straightforward. 

HealthKit is constantly being updated

There is a lot of documentation available for HealthKit, but even with that, we found the integration challenging. There are always nuances with any API or data source. HealthKit, like any iOS feature, could be updated with an operating system update. 

There were a few instances where we found a solution to something on a forum only to read the next response: “Oh yeah, that’s not how it works anymore.” Make sure you keep up-to-date on HealthKit documentation and are ready to update for new changes.

HealthKit can work differently on iOS versions

Because HealthKit changes with operating system updates, you must code for multiple iOS and device combinations. For example, Apple completely overhauled how they track sleep data in iOS 16. 

Apple is known for innovation, but that means you have to keep up with the constant changes in the platform.

Not everyone sleeps the same hours

Sleep data was one of the most difficult HealthKit categories to work with. Unlike steps, sleep data is category data—not quantity data. HealthKit also tracks multiple sleep data units, including in bed, asleep, REM, core, and deep sleep. 

If overlapping data exists, the query returns it, and you have to calculate the actual number of seconds asleep in the app. Sleep is also tracked from 6 PM to 6 AM the following day, which is different than how the other HealthKit categories are tracked. We had to adjust the query range for this specific HealthKit query to include the previous day.

HealthKit privacy can be tricky

One of the more challenging aspects of HealthKit from a user experience perspective is the permission flow. HealthKit requires that a user give permission for each data category that will be accessed or written. The issue is that if a user denies permission, you are not allowed to prompt them again. 

Not only that, but the app doesn’t know the user denied permission—that’s part of the privacy protections. If the user has denied permission, even accidentally, HealthKit returns an empty response. There’s no error message or status included.

What’s next?

Like any new release, we’re excited to dig into the app analytics to see how BrainFit users use HealthKit-connected habits. We’ve already had a few internal conversations about what to add next, and we’re excited to see what changes Apple has in store for HealthKit this year.

​​Ready to take your project to the next level? Our team is here to help. Contact us today to learn more.


June 17th, 2022 by Joe Reda
What you need to know from Apple WWDC 2022
June 21st, 2018 by
Waterloo Garbage & Yard Waste App
May 13th, 2021 by Alex Kinsella
How to get started with Bootstrap 5.0