Join Lee Brimelow for an in-depth discussion in this video Updating and accessing health data, part of iOS 8 App Development New Features.
- Now that we've looked at an overview of the HealthKit framework, let's look at an example of how to actually read and write health information. I've opened up the HealthKit sample starter project. This is basically an empty application, I've just done a couple of things. First I've turned on the HealthKit entitlement, which is obviously vital if you want to be able to read and write health information. In the view controller, I basically set up a couple of things. First I've imported the HealthKit framework and I've created a constant called HealthStore, which is of type HKHealthStore.
This is the main object that we use to interact with the HealthKit framework. The second thing I've defined is a constant called Type and here is where we define essentially which pieces of information that we actually want to read and write. In our example here we're just going to be dealing with weight. We're going to be reading weight records and also writing them. It's important that you actually have some health information on your device. I'm going to the iOS simulator here.
Now if you haven't used the health app before you're going to go into it and you're going to see an empty dashboard. That's because you haven't set anything up and you haven't saved any data here before. You want to go to health data and here is where you're going to choose all the various health stats that you want to track. In our example, we're just going to track weight. I'm going to choose show on dashboard, so that I see when I come into the health app a chart of all of the records and then I'm just going to create a couple of data points.
Right now I'm 220 pounds, sadly. I'll add that and we're going to add another data point which is about 15 seconds later and I will have magically lost five pounds and I'll add that. Essentially we have a couple of data points here, so we actually have some records to retrieve. Let's go back to our code. We're not going to do any UI on this application. We're simply going to retrieve information and I'm just going to print it out to the console so we can see it.
The first thing I have to do in here is to create an instance of NSSet, which I'll call types. The reason for this set is in a typical example you're probably going to want to be reading and writing a bunch of different types of health information, so you need to put all of those types into an NSSet object. In our example we're just tracking weight, so I'm just going to pass in that single type object. Before we can actually start requesting information, we first need to request access or authorization to the user.
Basically we need to say, is it all right for our application to access your health information. To do that we use the HealthStore.RequestAuthorizationToShareTypes. Here we pass in the types of information that we actually want to share and it's that NSSet we created called Types. The types that we want to read are the same, so we'll also pass in types. Now we're going to define our completion handler and this is going to pass in a Boolean whether the operation was successful, which we'll call Success or it could pass as an error if there was an error.
Inside this completion handler, we're just going to assume success. Now I have access, now I need to actually query the HealthStore. I'm going to create a new constant. I'll call it Q for query, and this is going to be of type HKSampleQuery. The first thing that we need to pass in is the type of information we want to access. That's going to be that constant self.type. For predicate we'll put nil because we want to get all of the items, we don't want to restrict it. So for a limit we'll put zero.
Sort descriptors we'll pass in nil, and then we need to define our results handler which is going to send us three things. First the query object, next the results that are returned. Of course it could give us an error, which will be held in this error argument. All I'm going to do here is to simply print out the results to the console. Here we've defined our query, we just now need to execute it on the HealthStore. To do that we're going to use the HealthStore.ExecuteQuery method and pass in our query object.
Let's go ahead and run that. You can see, right away, we get something that pops up and it's asking the user, is it okay to both allow this app to share and write weight information. We're going to agree and click done. Now if we look down at the console, we have our two records. Here is one of the records here and you can see it's actually giving us that information in kilograms, but of course we could convert it. But here are our two records. It's just that easy to actually request and retrieve health information from the HealthStore.
What if we actually want to write information? Let's look at that. I'm going to get rid of this query and what we're going to do once we know that we actually have authorization is we actually want to write a new weight sample to the HealthStore. I'm going to create a constant called Sample and this is going to be of type HKQuantitySample. First we're going to pass in which type that we need to actually use or we're going to use self.type.
For the quantity we can't just pass in a number, actually it has to be an instance of HKQuantity. Here we need to tell it what units we actually passing this value in as. This is going to be of type HKUnit.PoundUnit. Now we'll pass in the actual value. Let's say I've totally fallen of the wagon, we'll pass in 280. Now we need a start date and an end date. For this we can just simply pass in new instances of NSDate like that.
We've created this sample, now we just need to save it. We'll say Self.HealthStore.SaveObject, passing the sample that we've just created and we're not going to worry about a completion handler. Let's run it again. It launches, obviously nothing's happened, because all we're doing is saving a new health record to the HealthStore. Let's actually go back to the health app and you'll see here immediately that that new record has been entered at 280 pounds.
If we actually look, show all data, here we can see the record that was written by our app and like I said, I really let myself go on this one. That's the basics of both reading and retrieving health records and writing them using HealthKit.
- Getting started with Swift
- Using Swift playgrounds
- Exploring unified storyboards
- Understanding how extensions work
- Creating a Today widget
- Using the Photos framework
- Controlling graphics with the Metal framework
- Overview of HealthKit and HomeKit
- Combining Swift and Objective-C