CareKit Tutorial for iOS: Part 1

In the first part of our CareKit Tutorial for iOS, you’ll learn the basics of using CareKit by building a Zombie Training and Symptom Tracker. By Jeff Rames.

Leave a rating/review
Save for later
Share

In a zombie apocalypse (or any other time, really) there’s nothing as important as health. Thankfully we have CareKit, an open source framework designed for building iOS apps that help users manage and understand their personal health. CareKit apps help users create and follow health plans, track progress, analyze the resulting data and even share that data with care providers.

Thanks to Apple’s continued interest in improving personal health with open source software, along with the health tracking capabilities of the iPhone and Apple Watch, it’s never been easier to put together an app that can have a powerful impact on the lives of many people!

In this CareKit tutorial, you’ll leverage CareKit in an app aimed at users trying to stay infection-free during a zombie apocalypse. You’ll create a care plan consisting of treatments to minimize risk and assessments to track signs of infection. You’ll also learn how integrating ResearchKit and HealthKit can make CareKit much more powerful.

If you’re unfamiliar with ResearchKit tasks, you may benefit from reviewing our ResearchKit tutorial before starting.

In Part 1 of the CareKit tutorial you’ll work on Zombie Training and Symptom Tracker; Part 2 will cover Insights and Connect.

Grab some … err … your brains, and let’s dive in!

Getting Started

Download the ZombieKit starter project and open it in Xcode. This contains a stubbed-out Tab Bar Controller, ResearchKit and HealthKit, plus a few helpers you’ll learn about during this CareKit tutorial.

Build and run ZombieKit to see the placeholder Tab Bar Controller in all its glory.

CareKit tutorial

Note: The ResearchKit build in this starter is a pre-release version with Swift 3 support. As a result, you’ll see a few compile warnings on the ResearchKit target. Don’t worry about them—they won’t impact your work in this tutorial.

First, add the CareKit framework to your project. Head over to the CareKit repository on GitHub and download the stable_xcode80_candidate zip or clone that branch using the following terminal command:

git clone -b stable_xcode80_candidate --recurse-submodules https://github.com/carekit-apple/carekit.git
Note: As of this writing, stable_xcode80_candidate is your best bet for CareKit with Swift 3 and Xcode 8 compatibility. You’ll want to use stable, once Swift 3 changes make it there.

Drag the downloaded CareKit.xcodeproj from Finder into the Project Navigator under the Linked Frameworks group. It should look like this:

CareKit tutorial

Note: Don’t try to add a framework to a project while another Xcode project using that framework is open, because it won’t properly import. If your end result doesn’t look like the screenshot, close any other open projects and try again.

Select the ZombieKit target, then the General tab of the target editor. Add CareKit.framework under the Embedded Binaries section.

CareKit tutorial

To confirm that you’ve successfully added CareKit to the project, add the following to the top of TabBarViewController.swift:

import CareKit

Build and run. If the project builds successfully, you’re all set!

CareKit tutorial

Anatomy of a CareKit App

Functionally, CareKit consists of four main UI modules:

  1. Care Card defines and tracks actions meant to positively impact the user’s health. For example, a weight loss app might track minutes spent exercising each day.
  2. Symptom and Measurement Tracker defines and tracks symptoms or vitals. For instance, that weight loss app would track your weight each day.
  3. Insights allows you to chart the collected data as well as present written alerts or findings. The weight loss app could chart exercise against weight to help visualize the correlation.
  4. Connect enables communication with care providers and friends. The chart showing weight loss compared to exercise efforts could, for example, be put into PDF format and emailed to your physician.
Note: Remember, you’ll only work on Care Card and Symptom and Measurement Tracker in Part 1 of this CareKit tutorial. You’ll add Insights and Connect modules in Part 2.

Care Plan Store

The core of a CareKit app is the Care Plan Store, which manages and provides an interface to a database that persists the care plan. Its data are mapped to any connected Care Card and Symptom and Measurement Tracker controllers. When information is entered in views owned by these controllers, the Care Plan Store automatically picks it up.

The store handles two primary data types you’ll use throughout this CareKit tutorial:

  • Activities represent the activities users will perform to manage their care and track their condition. These are often referred to collectively as the care plan.
  • Events represent an individual occurrence of a scheduled activity. They are made up of the activity itself along with scheduling data, completion status and results when applicable.

Activities are further broken down into two types:

  • Intervention Activities are actions users take as part of their treatment. They appear on the Care Card.
  • Assessment Activities represent activities users take within the app to evaluate their health. These appear on the Symptom and Measurement Tracker.

You might have noticed that the Connect module wasn’t mentioned in the store. It actually doesn’t live there; instead, it’s up to you to persist OCKContact objects on your own.

The below chart shows the flow of information between the store, your contacts and the primary UI modules:

CareKit tutorial

Now that you’ve got the basics down, it’s time to set up the Care Plan Store so it persists training and tracking data in ZombieKit. Open CarePlanStoreManager.swift and replace import Foundation with:

import CareKit

You’ll use CareKit in this file, and it already includes Foundation.

Add the following property to the top of the CarePlanStoreManager class:

var store: OCKCarePlanStore

Next, add the following inside init(), just above super.init():

store = OCKCarePlanStore(persistenceDirectoryURL: storeURL)

This creates a Care Plan Store at storeURL in your documents directory. Note that the starter project includes a type property singleton called sharedCarePlanStoreManager—this allows you to access the store from anywhere in ZombieKit.

Build and run to confirm a successful build. You won’t notice any differences until you add data and start hooking up CareKit controllers.

CareKit tutorial

Care Card

In a zombie apocalypse, your users need to stay sharp. A great first step is to define a training plan and track adherence to it. CareKit has just the thing you need—the Care Card!

Open TabBarViewController.swift and add the following property to the top of the TabBarViewController class:

fileprivate let carePlanStoreManager = CarePlanStoreManager.sharedCarePlanStoreManager

This provides a pointer to the store you created in CarePlanStoreManager.

Find createCareCardStack() and replace this line:

let viewController = UIViewController()

With this:

let viewController = OCKCareCardViewController(carePlanStore: carePlanStoreManager.store)
viewController.maskImage = UIImage(named: "heart")
viewController.smallMaskImage = UIImage(named: "small-heart")
viewController.maskImageTintColor = UIColor.darkGreen()

This initializes an OCKCareCardViewController, the main controller for the Care Card, and passes a Care Plan Store to act as the data source. You also set the maskImage, smallMaskImage and maskImageTintColor with images and colors from the starter project that help zombify the look of the progress indicator. If these aren’t set, the Care Card progress image defaults to a red heart with red fill.

Build and run—your Care Card should show a header with weekly navigation and progress as well as a detail view with a larger progress indicator. Right now both show 100% because you’ve completed 100% of your non-existent Intervention Activities. You’re great at doing nothing!

CareKit tutorial