Apple launched the Health app in 2014. To allow developers to integrate the data held within Health, Apple provided the HealthKit framework. With permission from your app user, your app can read and write data to Health and provide a richer experience for users.
The articles below are designed to help you work with HealthKit and smoothly integrate it in to your app.
Before you get started on a HealthKit enabled app that you want to release to the app store, please take a read of the Protecting User Privacy article over at Apple. It will explain the do’s and dont’s of using private data.
How to Setup Apple HealthKit in Your App – If you are unfamiliar with how to setup HealthKit in your app, this is the best tutorial to begin with. It takes you through the 4 simple steps that are required to get started.
A Quick Look at HKHealthStore – This provides a look at some of the methods available in HKHealthStore that we do not address in the tutorials below.
Queries
The HealthKit framework allows you to pass in a query and fetch information back about the user. These are under strict control and require the user to authorise every single item of data. The tutorials listed below show the several ways that you can request data.
Reading Characteristic Data from HealthKit – A few of the data types stored in Apple Health are categorised as being characteristic data. These are typically items that do not change such as your birth date and sex. This tutorial shows you the quick way you can retrieve this information about the user.
HKSampleQuery Tutorial and Examples – The HKSampleQuery is a simple way to query data in Apple Health. This particular query is described by Apple as being the general-purpose query.
How to Use HealthKit HKAnchoredObjectQuery – The HKAnchoredObjectQuery is similar to the HKSampleQuery, but instead of having to make repeat requests to fetch data, you can set up an anchor so that you only fetch data that has been added or deleted since the anchor was set. This is particularly useful for apps that want to represent data over a period of time such as a weight tracking app. When a user makes a change to a sample, you will receive that update either when you run the query, or automatically if you configure it as a long-running query.
The HealthKit HKStatisticsQuery – The HKStatisticsQuery is used to perform calculations on quantity samples. It allows you to quickly find cumulative totals as well as discrete averages, max, and min values for a given type. Use this if you are interested in doing some analysis on the set of quantity samples of your choice.
How to Use the HKStatisticsCollectionQuery – This query is similar to the HKStatisticsQuery, although one major difference is that you can perform statistics calculations over intervals of data. This is especially useful for calculating step count totals on an hourly, daily, weekly basis.
How to use the HKSourceQuery – Perhaps one of the shorter tutorials on HealthKit queries. The HKSourceQuery is useful to find out what apps and devices have been updating a particular quantity sample type.
The HealthKit HKActivitySummaryQuery – Use this query to access the users activity rings for a specific date or range of dates. This includes access to the 3 types of data which are activeCalories, exercise, and stand hours, along with their associated goals. We also have a second tutorial that demonstrates the long-running query along with a demonstration of the HKActivityRingView.
Saving Data to HealthKit
Creating and Saving Data in HealthKit – We’ve looked at the queries that allow you to read data from the health store. Now it’s time to look at how we can save data. In this tutorial we look at how to save quantity type samples.
Saving Category Samples with HealthKit – The previous tutorial covered saving quantity samples. This tutorial show you how to record category samples. It also explains the difference between a category sample and a quantity sample.
More to Come
There are more types of queries to read data, and also ways to write data to Apple Health. Over the coming days and weeks, more tutorials will be added to the site.