Premier iOS Development Central

Introduction to CareKit, Part 3: Connect

Troy Tsubota • 06 January 2016

Welcome to the third part of the Introduction to CareKit tutorial. This final part of the tutorial will wrap up the introduction to the six CareKit modules.

In Part 1, you learned what CareKit is and used three modules: the Care Card, Symptom and Measurement Tracker, and Care Plan Store.

In Part 2, you used Insights to display a message and graph about the user’s sleep and care completion.

In this part, you’ll use the final two modules, Connect and Document Exporter, to add contacts and have the user be able to share a care PDF with them.

The Connect module

Adding Contacts

Contacts in CareKit are of type OCKContact, which have the following properties:

type: a contact can either be part of the user’s care team or one of their personal contacts. There isn’t much difference between them except that they’re separated in the view controller and you can restrict sending reports to only one type.

name

relation: “Mom”, “Dad”, “Doctor”, etc.

contactInfoItems

.phone

.sms

.email

.facetimeVideo: video number or email.

.facetimeAudio: audio number or email.

tintColor

monogram: one or two letters to represent a contact (defaults to first and last initial).

image: a profile picture of sort.

Open your HelloCareKit project from the second part. If you haven’t completed Part 2, download the finished project from that part on GitHub.

Adding contacts to the Connect module is really easy. First, define a property to store the contacts in the tab bar controller:

OCKConnectViewControllerDelegate

The Connect view controller delegate is used for sending reports to contacts (as well as custom handling of contact info items), which is disabled by default. Add this line to viewDidLoad, after declaring connect:

connect.delegate=self

Then, add an extension at the bottom of the file that conforms to OCKConnectViewControllerDelegate and implement two of the three available methods:

connectViewController(_ connectViewController:, didSelectShareButtonFor:, presentationSourceView:) is for when the share button is selected for a contact. presentationSourceView is provided for any view controller presentation. The second method, connectViewController(_ connectViewController:, titleForSharingCellFor:) is to give a title for the sharing table view cell. Returning nil disables sharing for the contact being passed through the method.

You’ll fill in the first method’s body in the next section.

Creating a PDF

The core of the Document Exporter is OCKDocument. A document has HTML content and can be converted to a PDF with a convenience method. A document made with CareKit can contain various OCKDocumentElements:

OCKDocumentElementSubtitle

OCKDocumentElementParagraph

OCKDocumentElementImage

OCKDocumentElementChart (note: there’s currently a CareKit bug where charts don’t render in both HTML and PDF forms of the document)

OCKDocumentElementTable

In the document you’re going to create, there’s going to be three elements: a paragraph, a subtitle, and a table. The table will include sleep data from the past week.

Add the following to connectViewController(_ connectViewController:, didSelectShareButtonFor:, presentationSourceView:):

That’s it for this 3-part tutorial to introduce you to CareKit! If you enjoyed this tutorial, be sure to follow us on Twitter and keep checking back here for more tutorials relating to CareKit and other iOS topics.

If you have any questions, feedback, or tutorial requests, feel free to open an issue on GitHub. I hope you enjoyed this tutorial!

Beachside Coders™ provides high quality resources for iOS development from comprehensive tutorials to consulting services.