Set up and configure your HealthKit store.
- Bundle Resources
Before using HealthKit, you must perform the following steps:
Enable HealthKit in your app.
Ensure that HealthKit is available on the current device.
Create your app’s HealthKit store.
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.
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.
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 file and delete the
healthkit entry from the Required device capabilities array. The
healthkit entry isn’t used by WatchKit extensions.
Ensure HealthKit’s Availability
is method to confirm that HealthKit is available on the user's device.
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
error error. If HealthKit is restricted (for example, in an enterprise environment), the methods fail with an
Create the HealthKit Store
If HealthKit is both enabled and available, instantiate an
HKHealth object for your app as shown:
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.