Nowadays health is one of the issues that most concern people and I’m sure all of you know at least one person who is a runner, am I wrong?
On the other hand, we know a lot of apps on the App Store that register the routes, distances, calories, and time spent of our physical activities and Apple has given a step forward in this direction: they have built and integrated into their iPhone6 and iPhone6 Plus devices a specific motion processor (the M8, big brother of the iPhone5S’ M7), and they also have developed and included in iOS8 an independent framework, HealthKit, to maximize its full potential.
This new framework helps communicating health data between applications and keeps it stored and privately protected until the user decides make it available.
What you need to know
If you are planning to develop an app that makes use of HealthKit you should take into account the following aspects:
- Enable the HealthKit capability in Xcode.
- Check the availability of HealthKit by calling the isHealthDataAvailable
- Create an instance of HKHealthStore, which is the responsable of interacting with the HealthKit data base.
- Request user’s authorization by calling the requestAuthorizationToShareTypes:readTypes:completion: This authorization can be checked at any time by calling authorizationStatusForType:.
At this point lets imagine that we are developing an app that controls the heart rate of the user and want to add some samples (HKSample), which are a representation of any kind of data, to the HealthKit store.
In this case we should create an HKObjectType (HKQuantityType specifically) by calling the quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyTemperature class method and then create a HKQuantitySample with the object type. Finally, we can save this date by calling the saveObject:withCompletion: method.
Besides quantity samples there are three more types of samples:
- Category samples: It represents data that can be classified into a finite set of categories, sleep analysis for example.
- Correlations: Complex data formed by one or more simple samples such us blood pressure or food.
- Workouts: Physical activities that are attributed by type, duration, distance, and energy burned.
- Quantity samples: As we have mentioned, the most common data such as blood pressure, respiratory rate, temperature, step counts, distance covered while walking or cycling, energy burned, body mass, height, or body mass index.
The final touches
Once the data is saved in the HealthKit store we can retrieve it in different ways:
- Reading characteristic data directly: By calling the biologicalSexWithError, bloodTypeWithError, or dateOfBirthWithError we can retrieve the characteristic data.
- Querying data by executing one of the different types of HKQuery:
- HKSampleQuery retrieves any type of data.
- HKAnchoredObjectQuery alerts of any changes on a specific data.
- HKCorrelationQuery retrieves data contained in a correlation.
- HKObserverQuery returns the data that has been last added.
- HKSourceQuery gets the apps and devices linked to the HealthKit store.
- HKStatisticsQuery exports statistics over a set of samples.
- HKStatisticsCollectionQuery executes multiple statistics queries.
After this overview you may have made a picture of the potential this framework provides but the best of all is that all this data traveling from devices to apps passing from HealthKit is made in a safe way as it provides ways to share this sensitive data just with trusted devices.
I hope this kit is used to develop amazing apps beyond fitness ones, such as medical care. Do you have any applications you would like to be created with HealthKit?