Chapter 3. Managing Health Data with HealthKit

3.0. Introduction

Apple has created a health management application, appropriately called Health, which allows the user to enter their health-related information. This application is backed by a database that programmers don’t have direct access to. However, a layer that is called HealthKit gives us access only to the information that we are authorized to use. Therefore, we need to ask the user for authorization before we can access their information.

There are more than 15 classes that we need to learn about in the HealthKit framework, and one of the most important is HKUnit. This class is responsible for representing units in various measures. For instance, one kilogram can be expressed as grams, milligrams, tons, etc. Also, this class allows you to convert units with ease. Let’s say that in your country, a person’s weight is measured in kilograms. But when you go to England, people ask you “How many stones do you weigh?” because they count weight in stones. The aforementioned class will help us a lot in working with various units, as we are going to see in Recipe 3.7.

3.1. Setting Up Your App for HealthKit

Problem

You want to be able to start using HealthKit in your app.

Solution

Follow these steps:

  1. Set up your provision profile as explained in Recipe 1.22.

  2. Import the HealthKit framework into your project.

  3. Navigate to the Capabilities section of your Xcode project and enable HealthKit as shown in Figure 3-1.

  4. Query the isHealthDataAvailable ...

Get iOS 8 Swift Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.