HealthKit is Apple’s bridge from the important, track-able health data to the health-conscious tech-consumer, exercise-fan, and regular person with an iPhone.
A user can easily track measurable fitness and health data over a period of time. Aside from the benefit of awareness, seeing the positive slope in the graph is inspiring and a great motivator.
What is HealthKit?
The HealthKit framework can collect, store, and organize all sorts of data regarding a person’s physical, mental, and even arguably spiritual state. The Health app you all see on your iPhone's is Apple’s portal to the framework, but developers can take advantage of the HealthKit API to create their own health-related apps.
Some data tracked by HealthKit can only be entered by the user and is read-only to developers (like date of birth). Other data, like a user’s heart rate and blood pressure, can be collected and written into the HealthKit by developers — and they can read that data, too.
HealthKit, that data is available to all other apps, but only if the user consents on an app-by-app and health-data-point-by-data-point basis. Users have very granular control over their health data.
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.
Creating a complete, personalized health and fitness experience includes a variety of tasks:
- Collecting and storing health and fitness data
- Analyzing and visualizing the data
- Enabling social interactions
- HealthKit Data
HealthKit saves a variety of data types in the HealthKit Store:
Characteristic data: These records represent items that typically do not change, such as the user’s birth-date, blood type, biological sex, and skin type. You can read this data directly from the HealthKit store, using the date, blood, biological, and Fitzpatrick methods. Your application cannot save characteristic data. The user must enter or modify this data using the Health app.
Sample data: Most of the user’s health data is stored in samples that represent data at a particular point in time. All sample classes are sub-classes of the HKSample class, which is a subclass of the HKObject class.
Workout data: Information about fitness and exercise activities are stored as HKWorkoutsamples. While HKWorkout is a subclass of HKSample, it behaves somewhat differently than other sample subclasses. These differences are described in Workout Data.
Source data: Each sample stores information about its source. The HKSourceobject contains information about the app or device that saved the sample. The HKDeviceobject contains information about the hardware device that generated the data.
Deleted objects.
An HKDeleted instance is used to temporarily store the UUID of an item that has been deleted from the HealthKit store.
You can use deleted objects to respond when an object is deleted by the user or another app. For more information, see HKAnchored and HKDeleted.
Setting Up Health
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.Enable HealthKit
Before you can use HealthKit, you must add the HealthKit capabilities for your app.
In Xcode, select the project and turn on the HealthKit capability. Only enable the Health Records checkbox if your app needs to access the user’s clinical records.
If HealthKit is not required for the correct operation of your app, you can open the app’s Info file and delete the health kit entry from the Required device capabilities array. The health kit entry is not used by WatchKit extensions.
Ensure HealthKit’s Availability
Call the is a method to confirm that HealthKit is available on the user's device.
if HKHealthStore.isHealthDataAvailable(){
}
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. If HealthKit is restricted (for example, in an enterprise environment), the methods fail with an error.
Create the HealthKit Store
If HealthKit is both enabled and available, instantiate an HKHealth object for your app as shown:
let health store = 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.
Request Permission to Read and Share Data: To help protect the user’s privacy, HealthKit requires fine-grained authorization. You must request permission to both reads and share each data type used by your app before you attempt to access or save the data.However, you do not need to request permission for all data types at once. Instead, it may make more sense to wait until you need to access the data before asking for permission.
The example below shows the SpeedySloth app asking for permission to read and share energy burned, cycling distance, walking or running distance, and heart rate samples.
let allTypes = Set([HKObjectType.workoutType(),
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKObjectType.quantityType(forIdentifier: .distanceCycling)!,
HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!,
HKObjectType.quantityType(forIdentifier: .heartRate)!])
healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
if !success {
// Handle the error here.
}
}
Any time your app requests new permissions, the system displays a form with all the requested data types shown.
You must also provide custom messages for the permissions sheet. A separate custom message is required for both reading and writing HealthKit data. In your app’s Info, set the key to customizing the message for reading data. Set the NSHealthUpdateUsageDescription key to customizing the message for writing data.
About Author
Subscribe to Our Newsletter!
Join us to stay updated with our latest blog updates, marketing tips, service tips, trends, news and announcements!