Article

Setting Up HealthKit

Set up and configure your HealthKit store.

Overview

Before using HealthKit, you must perform the following steps:

  1. Enable HealthKit in your app.

  2. Ensure that HealthKit is available on the current device.

  3. Create your app’s HealthKit store.

  4. Request permission to read and share data.

The first three steps are described in detail below. For more information on requesting authorization, see Authorizing Access to Health Data. For a practical example of how to set up and use HealthKit, see SpeedySloth: Creating a Workout.

Enable HealthKit

Before you can use HealthKit, you must enable the HealthKit capabilities for your app. In Xcode, select the project and add the HealthKit capability (see Figure 1). Only select the Clinical Health Records checkbox if your app needs to access the user’s clinical records. App Review may reject apps that enable the Clinical Health Records capability if the app doesn’t actually use the health record data. For more information, see Accessing Health Records.

Figure 1

Enable HealthKit capabilities

A screenshot of Xcode, with the Signing & Capabilities tab showing the HealthKit capabilities. The Clinical Health Records checkbox is not selected.

For a detailed discussion about enabling capabilities, see Configure HealthKit in Xcode Help.

When you enable the HealthKit capabilities on an iOS app, Xcode adds HealthKit to the list of required device capabilities, which prevents users from purchasing or installing the app on devices that don’t support HealthKit.

If HealthKit isn’t required for the correct operation of your app, you can open the app’s Info.plist file and delete the healthkit entry from the Required device capabilities array. The healthkit entry isn’t used by WatchKit extensions.

For more information on required device capabilities, see the UIRequiredDeviceCapabilities key in Information Property List Key Reference.

Ensure HealthKit’s Availability

Call the isHealthDataAvailable() method to confirm that HealthKit is available on the user's device.

if HKHealthStore.isHealthDataAvailable() {
    // Add code to use HealthKit here.
}

Call this method before calling any other HealthKit methods. If HealthKit is not available on the device (for example, on an iPad), other HealthKit methods fail with an errorHealthDataUnavailable error. If HealthKit is restricted (for example, in an enterprise environment), the methods fail with an errorHealthDataRestricted error.

Create the HealthKit Store

If HealthKit is both enabled and available, instantiate an HKHealthStore object for your app as shown:

let healthStore = HKHealthStore()

You need only a single HealthKit store per app. These are long-lived objects; you create the store once, and keep a reference for later use.

Topics

Entitlements

HealthKit Entitlement

A Boolean value that indicates whether the app may request user authorization to access health and activity data that appears in the Health app.

Key: com.apple.developer.healthkit
HealthKit Capabilities Entitlement

Health data types that require additional permission.

Key: com.apple.developer.healthkit.access

Information Property List Keys

property list key NSHealthUpdateUsageDescription

A message to the user that explains why the app requested permission to save samples to the HealthKit store.

Name: Privacy - Health Update Usage Description
property list key NSHealthShareUsageDescription

A message to the user that explains why the app requested permission to read samples from the HealthKit store.

Name: Privacy - Health Share Usage Description
property list key NSHealthRequiredReadAuthorizationTypeIdentifiers

The clinical record data types that your app must get permission to read.

property list key NSHealthClinicalHealthRecordsShareUsageDescription

A message to the user that explains why the app requested permission to read clinical records.

Name: Privacy - Health Records Usage Description

See Also

Health

property list key NSHealthClinicalHealthRecordsShareUsageDescription

A message to the user that explains why the app requested permission to read clinical records.

Name: Privacy - Health Records Usage Description
property list key NSHealthShareUsageDescription

A message to the user that explains why the app requested permission to read samples from the HealthKit store.

Name: Privacy - Health Share Usage Description
property list key NSHealthUpdateUsageDescription

A message to the user that explains why the app requested permission to save samples to the HealthKit store.

Name: Privacy - Health Update Usage Description
property list key NSHealthRequiredReadAuthorizationTypeIdentifiers

The clinical record data types that your app must get permission to read.