skip to Main Content
Health Kit IOS App Tutorial Convert Your Tracking App

How to add Health Kit into your iOS app

In iOS 8, Apple introduced a new framework called Health Kit which allows iOS developers to create iOS apps that interact with accessories or the iPhone’s built in Motion sensors or GPS module.  Health Kit provides a lot of possibility for what it can be accomplished, today I’m gonna show you a simple and quick way of converting your Route Tracking app into a Health Kit provider.

Convert your location tracking iOS app to a fitness app

I’m gonna show you how you can easily convert your iOS app that tracks the user location into a fitness app, which will be able to save data to Health Kit so whenever your users are using your app to track their daily run, they will be able to see their results in the Health Kit app on their iPhone.

Basic setups

First as always, we need to create a new project, or if you have your project already created than open it.

Import HealthKit, optionally CoreLocation and MapKit if you haven’t got these last two already, since we are going to place the tracked route on the mapView and track the user’s location, so it is essential to have the CoreLocation framework imported, otherwise it won’t work.

Check out the previous tutorial and download the sample file to save some time.

Adding Health Kit

First you need to add the Health Kit framework to your project, by going to your Project file, select your Target, from there select the General tab and go to the section called: Linked Frameworks and Libraries. Click the plus button and search for the framework. While your are here, you can also turn on Health Kit at the Capabilities tab to setup the entitlements, link the framework etc.

Now you are pretty much set up, if build & run now, nothing will happen regarding Health Kit, we need to do a little bit of coding to enable the feature.

Convert your tracking app in a fitness app

When I’m programming I like to have everything settled and organized, that’s why we are going to create a Health Kit manager object, which will manage all the savings and readings (only savings in this tutorial) for us.

So, create a new object and name it HealthKitManager, subclass it from NSObject.

Subclass health kit object from NSObject

Than we import HealthKit framework and setup a HealthStore property.

And add a method, which will check if Health kit is available or not for the current device. Obviously if it is not available, than it doesn’t make sense to do any savings and call the methods.

Next we need to request an authorization from the Health Kit framework to gain read and/or write access.

Gain read or write access from Health Kit framework

Than we need to setup Health Kit if possible, and request for write and read writes. In our case we will only request for writing rights, since we are only writing distance values.

As you can see, what it does is, it asks if Health Kit is available, if so than request the health store for writing rights. That’s when the view pops up. Than return with a success Bool and error is there was any. Inside this method we have another one, a getter, which defines what rights we need to write or read from Health Kit.

It returns a NSSet of HKObjectType objects, in our case only this one.

 Saving to Health Kit

Let’s get to the fun part, saving our distance values. What we need is a method that saves to health store and creates a HKQuantitySample object, cause only those objects can be saved to Health Kit.

I wrote a simple method that converts our distance value (e.g.: 12,6 km) into a HKQuantitySample object. Looks like this:

We create a quantityType to identify what we want to save, define the unit, in this case meter and set the distance. You can also set the timeframe when the running happened, by setting the start and end dates.

 Saving the distance value to Health Kit

Only thing left is to save the distance value to Health Kit and we are done, you are doing that by calling the saveObject method on the health store property.

That’s it. Now you are able to track your location and save it to Health Kit.

  • Johnny Bicycle

    Have you been able to load health app permissions modal from a button?

    • Sorry missed your question. 🙂 Of course, just need to call healthStore.requestAuthorizationToShareTypes(… ) in your IBAction method.

  • Krunal Chauhan

    Download Link not working, even if I liked through Facebook & Shared with my GPlus Account.
    So I am thinking that your Download Resource extension isn’t working properly.

    • Sorry about that, having some issues with the broken urls, moved from one server to another and that’s what causes this trouble. Will be fixed soon.