Developing an iOS application with an Apple HealthKit implementation.
Every day new health apps are being deployed in the App Store and day by day we can track more about our health through our phones and smartwatches. In this article, I will take you through my first Swift experience where I developed an iOS and watchOS application that measures your chronic physiological stress level based on HealthKit data recorded by the Apple Watch and guide you so that you can create your Health app too!
In case you have no idea what HealthKit is, no problem! I advise you to take a look at the Apple HealthKit documentation.
Points I will cover in this article:
- Wireframes and design
- Setting everything up for development
- Developing the UI
- HealthKit Authorization
- Reading data from HealthKit
Wireframes and design
Although wireframes and design are most of the times not the task of a developer. It might be an important and interesting mention for individuals that are starting a development project in the Apple world.
Creating wireframes and a design can help a lot with the development itself since you already thought about everything and only have an example to follow while building the UI in Swift. Apple, of course, wants us to remain in their styling and idea, so if you are planning on deploying your app to the App store, I advise you to take a look at the Human Interface Guidelines.
There is great software out there for creating wireframes and designs, the most known ones are Sketch, Figma and Adobe XD. Apple has also created great design resources, that make designing apps a really simple task.
Setting everything up for development
If we want to develop for Apple platforms, there are two requirements. The first one, you need an Apple Macbook or Mac. The second requirement is that you have to install Xcode from the App Store on your Mac device. The download of Xcode could take some time since it is pretty big. But that’s for a reason, Xcode is an IDE that comes with everything you need, EVERYTHING. From simulators to the compilers and SDK’s, for all Apple platforms. So when you have Xcode installed, you are ready to step into the Apple world.
Upon opening Xcode, you can create a new project. For my project, I chose for an iOS App with Watch App, since I wanted to develop an application for both platforms. But you can just choose the standard iOS app as well. Next up are some choices about the project settings. For the interface, I chose SwiftUI, for the life cycle the SwiftUI app and language of course Swift. The reason I chose Swift UI on these options here, is because it is more recent and Apple sees it as a big part of the future within the Apple world. Once you initialised your project, you are ready to start building your UI!
Developing the UI
Now that we have created the project, it is time to start building the UI. Developing a UI with SwiftUI requires you to have a little knowledge about how Swift and SwiftUI works and I will not cover that in this article, but instead I will show you how it can be done really easily. As I said previously, Xcode comes with everything and there are some great features here: the canvas and the snippets part. With just these two, you are able to build the UI in minutes.
The canvas is a live preview of your interface, without running on a device or a simulator. If you initialise your project, you will normally see it on the left, with of course the Hello World! text in it.
The next feature is the snippets part and this is just amazing. It allows you to just click and drag UI parts to your canvas or code. In this way you can build your UI by just dragging from the snippets part. But even if we want to customise it, we don’t have to write a single line of code by ourself. Because when you select some item in your code or canvas, you can just customise how much you want by just using the attributes selector on the selector panel, on the right, and add modifiers on the bottom. You can access the snippets with clicking the “+” sign on the right top.
To protect the privacy and health data of the user, you have to ask for access to the user’s HealthKit. In HealthKit you can read data from the HealthKit store and write data to the store from your app. But you have to ask access separately based on what you wish to do. Remember to only ask for the Health data that you need, otherwise your users might think negatively about your app and what your intentions are with their data. You don’t have to ask about their teeth brushing data when you are creating an app around heart-related data or workouts.
But first, let’s check if the user has HealthKit available on the current device. Because HealthKit is not meant for all platforms and older versions. If HealthKit is not available on the device, you have to handle this in the app. We can do this in the following way:
Now that we have checked if HealthKit is available on the device. It is time to ask for the user’s access to the HealthKit store on the device.
While developing this and testing it, you might get errors about keys. This is because we are required to add some information on why we need the data. This information will be shown on the authorization page on the device. We can add this in the info.plist file. Just add two keys with your description on why you exactly need the data.
This is all we have to do to ask for the HealthKit access. Reminder to not ask more datatypes from the HealthKit store then you need in your application. Now that we access to HealthKit, let’s move on and retrieve some health data!
Reading data from HealthKit
There are 3 possibilities in reading data from HealthKit.
- Direct method calls
- Long-running queries
The queries have many possibilities and different kinds of queries. In this article, I will only cover the sample query, since I think it is one of the most used queries. And you will be able to call other queries if you understand this one.
Sample query. This is a general-purpose query. Use sample queries to access any type of sample data. Sample queries are particularly useful when you want to sort the results or limit the total number of samples returned. For more information, see
HKSampleQuery.- Apple Documentation
First, we have our sample type, this is the data type we want to request from HealthKit. All data types can be found here.
Next, we have our predicate, which defines the period of the samples that we want to retrieve.
Then you have the limit of samples that can be set, if you want no limit, you have to insert 0, not nil.
And finally, we have our sort descriptor, which defines how we want to sort the recorded data.
Now that we have seen everything needed for the sample query, let’s call it with the initiated healthstore!
And that’s all you need to start developing your own Health iOS application using HealthKit. Best of luck building your own Health app and making the world a better place!