Apple Watch HealthKit Developer Tutorial: How to Build a Workout App

Introduction to Workout Apps

Having a healthier lifestyle is a challenge for many people; most of the time it is because we forget to stand up and move. That is why workout and exercise apps are having a lot of success in the App Store. It is a market that moves millions of dollars a year. In this post, we are going to explain how to build a workout tracker App to improve our health and our quality of life.

 

Project Description

The goal of the app is to allow users to create workouts like walking and running, select a date and time to schedule those workouts, and receive workout reminders to help them stay on track. Also, we wanted to provide users with a way to track their heart rate (BPM – beats per minute) and the amount of calories they burned while exercising. This is where the Apple Watch sensors come into play.

 

Technical Details

In this HealthKit / Apple Watch overview, we will walk you through some of the code we used in this project and some of the challenges we faced:

  • Brief introduction to HealthKit
  • Setting up HealthKit iOS project
  • How to request permission and access HealthKit data
  • Retrieving data from HealthKit
  • Brief introduction to Apple Watch development (watchOS)
  • Setting up Apple Watch
  • Communication between Apple Watch app and the iPhone app
  • HealthKit + Apple Watch

We will be using several Apple tools to build, run and test our app.

 

Hardware:

  • iPhone
  • Apple Watch

 

Software, Language and Libraries:

  • Swift
  • HealthKit
  • WatchKit

 

Project Architecture

Since the Apple Watch is still fairly dependent on connection to an iPhone (although less so with the series 3), the iPhone app is in charge of keeping track of the workouts, creating new workouts, starting a workout and displaying one’s progress on the screen. That means we need to store the data and keep the status of the workout in sync with the Apple Watch. The app employs the Apple Watch for quick interactions, such as selecting a workout, and utilizes its heart rate sensor to monitor the user’s heart rate. Below, you will find the workout list screen that keeps track of active, upcoming and past workouts. The data is stored using CoreData, and data is updated in real time for active workouts via communication with the watch.

Workout App

4 Steps to Set Up a HealthKit Integration Project

Setting up a project that integrates HealthKit is simple with these hands-on steps!

Step 1. Create the iPhone App and the Watch Extension

Open Xcode and create a new project, then enable HealthKit so that you can track the user data.   

Xcode

Health-Kit

Like other personal user information (photos, location, etc.), HealthKit requires user permission. After we set up our project and enable HealthKit, we are ready to request access to it. We are going to do so by using HKHealthStore.isHealthDataAvailable().

Step 2. Request the user’s authorization by calling the requestAuthorizationToShareTypes:

readTypes: completion: method

Since users can update their authorization status in settings, this value can be checked at any time by calling authorizationStatusForType.

Step 3. Create an instance of HKHealthStore, which is responsible for interacting with the HealthKit data base.

Step 4. After we create the iOS App, we can continue adding the Watch Extension to the project. In this section, we are going to create the actual watch app, the UI, and the code to collect the heart rate data.

 

To add a watch app to your existing iOS app project in Xcode, open the project for your iOS app. Choose File > New > Target, and navigate to the watchOS section.

WatchKit

This action generates two folders, AppName WatchKit Extension and AppName WatchKit App, in our project navigator.

The interface for the watch app will take place in the WatchKit app storyboard, and the extension will contain all the code.

storyboard

Retrieving Data from HealthKit

HealthKit provides push and pull-based queries for retrieving data.

  • Push-based Queries: Return a result every time an event is logged.
  • Pull-based Queries: Return a result just once, after being initiated.

To successfully execute a query, either push- or pull-based, with HealthKit, you need to follow the three steps below:

  • Define a HKQuery object.
  • Execute the query on the HKHealthStore object to obtain results.
  • Define an HKQuery object with a type matching the result type (such as statistics, samples, cloud-backed data, etc.) and the desired execution method (push or pull).

HKQuery is an abstract class used to find specific data from the HealthKit store.

Table 1, below, explains the most important HKQuery subclasses.

HKSampleQuery

Immutable query that searches for sample data using a specific filter (time and quantity) and sorted results in a specific order.

Pull (1 event)

HKStatisticsQuery

Immutable query that executes statistical calculations (i.e. minimum, maximum, average, sum for cumulative quantities, etc) for quantity samples objects, returns

Pull

HKStatisticsCollectionQuery

Mostly immutable query that executes multiple statistical calculations (i.e. total number of steps for a day, etc) for quantity sample objects. It returns a collection of results.

Pull

HKObserverQuery

Immutable query that observes and responds to changes to a quantity object (i.e. step count data changes)

Push (generates events until stopped)

HKAnchoredObjectQuery

Mostly immutable query that uses “anchors – holders” to retrieve the most recent data in the HealthKit store.

Pull

 

Adapted from Apple’s documentation regarding HealthKit sample types. 

 

Communication between the Apple Watch App and the iPhone App

 

Sync Manager

For the communication between the Apple Watch and the iOS App, we need a sync manager on both sides of the project. That means we need to have a sync manager on the watch side and the iPhone side. The way Apple supports communication between the two of them is the WatchConnectivity framework, which has delegate methods to send and receive data in real time (when both apps are in the foreground) or coordinated in the background (when one or both apps are in the background).

This sync manager relies on the WCSession to create a communication session between the iPhone and the watch. This session is the one sending and receiving data. The type of data that we can send is limited to system classes. To send and receive data from one side to the other, we use the following methods (depending on the cases mentioned before).

 

WatchConnectivity

This framework is available on both iOS and watchOS. It is the mechanism for sending information bidirectionally between the Apple Watch and the iPhone.

 

WCSession

The session used for communication between the app and the extension is a WCSession. To get real time updates from the watch, we have to coordinate the sync managers in the iOS app and the watch extension. That way, we can get updates even when the app is not in the foreground.

 

 

Getting Real-Time Updates

To read the current user’s health data, we created observers for the real-time updates.

One of the challenges we encountered when retrieving heart rate was the definition of which was the best / optimal HKQuery to use. In the end, and after doing some research, we opted to use HKObserverQuery. This query lets us know when the heart rate value has been updated.

To initialize the HKObserverQuery, we use the method HKObserverQuery(sampleType: HKSampleType, predicate: NSPredicate?, updateHandler: @escaping (HKObserverQuery, HealthKit.HKObserverQueryCompletionHandler, Error?) -> Swift.Void

For the sample type, we used the HKQuantityType object corresponding to heart rate (all HKQuantityType objects are subclasses of HKSampleType).

You do not need to specify a predicate for the query, because you do not need to filter the events. Every change to the heart rate will be observed.

Regarding the obtention of real time calorie updates during a workout, we triggered an HKAnchoredObjectQuery which uses an “anchor-holder” to retrieve the most recent data from the HealthKit store.

 

To initialize the HKAnchoredObjectQuery, we use the method HKAnchoredObjectQuery (type: HKSampleType, predicate: NSPredicate?, anchor: HKQueryAnchor?, limit: Int, resultsHandler handler: @escaping (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Swift.Void)

 

For the quantityTypeIdentifier, we used the HKQuantityTypeIdentifier.activeEnergyBurned object corresponding to cumulative energy / calories.

Important things to Keep in Mind:

HealthKit: This is a shared “data base” of knowledge across the system, which means we have access to all the data recorded in it. As a result, we have a lot of data to play with. It is also important to keep in mind that the iPhone and the Apple Watch have their own data, and iOS is the one in charge of synchronizing that data.

WatchKit: Building an elaborate user interface for the Apple Watch can be a very tricky and complex task to accomplish since the UI for the watch is a stack base UI. A stack base UI uses stacks to accommodate the controls on the screen and can be frustrating at first if you don’t have the expertise. This is much more limited compared to regular xib / Storyboards UI that iOS developers are used to working with.

Sync: Keeping the data updated on both sides of the app (iPhone and Apple Watch) is not as complicated as it sounds, but it is confusing at first. Once you understand the sync flow, though, the data transfer is easy to manage.

Interested in more “How To” posts? Check out our IoT Success Series!

Subscribe to our Blog
 

Danis Matiaz
Danis Matiaz
Danis is a Senior iOS Developer at Gorilla Logic.

Deliver off-the-chart results.

WordPress Video Lightbox Plugin