5 easy steps to integrate HealthKit

Whether you want to create a new HealthKit-enabled app or update your existing fitness or health app, this tutorial shows you how to easily integrate the new HealthKit API. This allows you to use all benefits of the iOS Health app, including storing your health and fitness data as well as reading health and fitness data of other apps.

As promised, there are only five easy steps to integrate HealthKit into your app. The five steps are:

  1. Enable HealthKit
  2. Check availability
  3. Request authorization
  4. Read/Write some data
  5. Prepare for publishing

Next, we go through each step and explain it in a little bit more detail.

 

Enable HealthKit

First go to your target settings within XCode and head over to the capabilities page. Enable HealthKit and XCode will do the rest for you (e.g. adding the HealthKit framework, etc.).

Enable HealthKit

 

Note: When enabling the HealthKit capability, XCode adds a new device requirement healthkit  in your entitlement file. If you are updating an existing app with the HealthKit integration, you need to remove this requirement again. It is not possible to add any further device requirements to your app if it is already published on the AppStore. If you forget to remove it, iTunesConnect will give you an error when you try to select a binary for your app submission.

 

Check availability

As a staring point keep in mind, that you should check the availability of HealthKit in any location you wish to use the API. Since the Health app is (currently) only available on iPhones (and not on iPads or iPods) this is a inevitable task. However, the code is quite easy:

Note: HealthKit is available on iOS 8.0 and later. To be downward compatible the code above also checks if the class HKHealthStore is available at all. You should implement a static method, so you can use this code whenever needed.

 

Request authorization

Since HealthKit stores user sensitive data and Apple keeps an eye on users privacy, your app needs to request access to each data type separately. So you have to decide which data types you want to read  or read/write  (Apple calls the second mode share ).

For all main API calls you need an instance of HKHealthStore. Apple recommends, that you create only one instance of HKHealthStore  during the lifetime of your app. For this case you should use the singleton pattern. If you are not familiar with it, have a look at this note on Objective-C singletons.

Requesting access is easy as pie. Use the method requestAuthorizationToShareTypes: readTypes: completion: and specify which data types you want to share and read. For this you have to create NSSets  of HKObjectType  objects and each object represents a specific data type (e.g. body weight, biological sex, date of birth, number of steps, etc.). Each data type is represented by one of the concrete subclasses HKCategoryType, HKCharacteristicType, HKCorrelationType, HKQuantityType, or HKWorkoutType . For more information, please have a look at the official HKObjectType class reference.

The following example is pretty self explaining and requests access to read and share several data types at once.

This code above will present an authorization dialog to your users which will look like the following:

HealthKit Request Authorization dialog

The user in turn is now able to accept or decline each request for reading/sharing the data types. After confirming or canceling the dialog, your completion block will be executed and you can go on with your specific implementation.

Note: As I mentioned before, Apple has an eye on the users privacy. In the case of requesting access to health data, the API won’t tell you if you got access to read/share a specific data type or not. However, you need to act in every case like the user granted you access to it. For instance, if the user declined your app the access to the data of step count and you try to read the step count for the last days, the API won’t produce any error but you would receive an empty list.

 

Read/Write some data

Reading and writing data from and to the health store is really straight forward. The HKHealthStore class provides some convenient methods for reading basic characteristics. If you want to query some more complex data you have to use the corresponding subclasses of HKQuery. Below you will find three examples explaining some common use cases.

 

1. Query the biological sex of the user

2. Query the step count within a given time span of the user

3. Update the current weight of the user

Prepare for publishing

When you are ready to submit your new or updated app, there are only a few additional things you have to consider:

  • If not already done, enable HealthKit on your app id within the Apple iOS Developer Center.
  • Update your AppStore promotional text of your app and mention why and in what extend your app does connect to and use the Health App. The omission of updating you app description leads to app review rejection.
  • Provide a link to your app specific privacy statement. This is another prerequisite for a successful app review. To get further information about this topic, please review the section HealthKit and Privacy at the HealthKit Framework Reference site.

 

That’s it! I hope you got a quick insight into the topic and you enjoyed this HealthKit API tutorial.

5 responses to “5 easy steps to integrate HealthKit”

  1. Hy I am getting error on this statement.

    if(!error && results)

  2. […] iOS 8 HealthKit: Working with Biometric Data. 5 easy steps to integrate HealthKit – JadeMind. […]

  3. I am just integrating HealthKit in my app http://www.routieapp.com and this comes in handy. Thanks Andreas!

  4. […] am having a hard time to get HealthKit working for my iOS App. I have done all the steps I have found so far and none seem to solve my problem I keep getting this error when trying to authorize […]

  5. This is my first look at HealthKit, and this was a great article. Thank you for posting.

Leave a Reply

Your email address will not be published. Required fields are marked *