🏥 Receiving User Data with HealthKit

Jackie Leonardy é„­
5 min readJun 26, 2021

--

Hi! Have you ever try to ask for a user’s Health credentials data like blood type, biological sex, weight, height, and some other personal data? Well, you might need to get to know HealthKit. So, what is HealthKit? The keyword is here, according to the Apple Developer documentation

Access and share health and fitness data while maintaining the user’s privacy and control. HealthKit provides a central repository for health and fitness data on iPhone and Apple Watch. With the user’s permission, apps communicate with the HealthKit store to access and share this data.

https://flaskapp.com/zones/img/help06_01@2x.png

HealthKit is also designed to manage and merge data from multiple sources. For example, users can view and manage all of their data in the Health App, including adding data, deleting data, and changing an app’s permissions.

To put it in short, we use HealthKit as the gate to access all of the user’s Health credentials via one and only the Health App.

Requesting Health Authorization

Before using HealthKit, we must perform the following steps:

1. Enable HealthKit in our app (via Xcode)

Enabling HealthKit Capability

2. Adding and asking for the Health privacy request

Privacy Permission

Inside the App’s Info.plist, we request for the user’s Health privacy to share and update the data via HealthKit.

3. Check and ensure that HealthKit is available on the device.

We create a variable from the HealthKit singleton, then check if the device provides the Health Data / Health App.

Request Permission to Read and Share Data

Here, we’ll try to read some data from the User, such as the Date of Birth, Height, Sex, dan Weight. There are some differences between the method to retrieve the data based on the data type.

Apple Developer Academy: Introduction to HealthKit (2014)

We’ll only discuss in a brief some of the data types. The data types used by HealthKit are listed on the HealthKit Data Types page, HKObjectType overview, and Workouts and Activity Rings topics.

As the data that we will use, the DoB and Biological Sex will be categorized as the HKCharacteristicTypeIdentifier since it won’t be changing accordingly. Then, the weight and height will be categorized as the HKQuantityTypeIdentifier.

Let’s Code! 🔥

If we execute the code, the HealthKit access UI will pop up like this, what we need to note is that if there are particular health data that the user denies, it can only be enabled through Settings.

Read and Retrieve the User’s Health Data

As we’ve discussed above, the method to retrieve the Health Data depends on the data type of the object. We are also restricted to perform certain actions, such as saving or creating new data.

1. HKCharacteristicType đźš‘

Unlike the other object types, characteristic types cannot be used to create and save new HealthKit objects. Instead, users must enter and edit their characteristic data using the Health app. Similarly, you cannot create queries for characteristic types. Instead, use the HealthKit store to access the data (see Reading Characteristic Data).

Characteristic data. These records represent items that typically do not change, such as the user’s birthdate, blood type, biological sex, and skin type. You can read this data directly from the HealthKit store, using the dateOfBirth(), bloodType(), biologicalSex(), and fitzpatrickSkinType() methods. Your application cannot save characteristic data. The user must enter or modify this data using the Health app.

So, to retrieve the DoB and Biological Sex data, we could perform a direct method calls cation.

Direct method calls. The HealthKit store provides methods to directly access characteristic data. These methods can be used only to access characteristic data. For more information, see HKHealthStore.

Since the biological sex has some other options (blood type also), we need to access the raw value and specify the return string.

2. HKQuantityTypeIdentifier đźš‘

Since the quantityTypeIdentifier data changes over time, we will need to use Query to get the latest data because the return result is an array of QuantitySample.

Queries return the current snapshot of the data in the HealthKit store. All queries run on an anonymous background queue. When the query is complete, it executes the results handler on the background queue. HealthKit provides different types of queries, each designed to return different types of data from the HealthKit store.

Sample query. This is a general-purpose query. Use sample queries to access any type of sample data. Sample queries are particularly useful when you want to sort the results or limit the total number of samples returned. For more information, see HKSampleQuery.

Apple Documentation: Saving Data to HealthKit

Using HealthKit is not only limited to retrieving Health Data from the user, but we could also explore more on it, like creating a workout session or combining HealthKit with CareKit and ResearchKit. Explore more on here.

Thank you! đź‘‹

--

--