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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
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.
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
Drag the downloaded CareKit.xcodeproj from Finder into the Project Navigator under the Linked Frameworks group. It should look like this:
Select the ZombieKit target, then the General tab of the target editor. Add CareKit.framework under the Embedded Binaries section.
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!
Anatomy of a CareKit App
Functionally, CareKit consists of four main UI modules:
- 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.
- Symptom and Measurement Tracker defines and tracks symptoms or vitals. For instance, that weight loss app would track your weight each day.
- 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.
- 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.
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:
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.
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!