HealthKit Data: Reading/Writing

The blog is regarding Health application in iOS and how to read/write data from/to the Health application. We’ll see APIs of HealthKit framework.

Contents

We’ll see how can we read/write data from/to Health application.

Points

1. Create Project in Xcode

2. Initial setup

3. Create Shared Manager (HealthKitManager)

4. Get Write Permissions

5. Write Data to Health Application

6. Get Read Permissions

7. Read Data from Health Application

8. Keep things in mind.


Let’s begin.

1. Create Project in Xcode

  • Create new project with Single View Application
  • Setup project details

2. Initial setup

a. Add HealthKit Framework.

Select Target > Build Phases > Link Binary With Libraries > click ‘+’ > Type healthkit and add.

b. Before we go into code, we need to understand terms equivalent to read and write.

Read = Sharing Health app data with your app

Write = Update Health app data from your app

We need to give reason that why we want to read or write data from or to Health application.

So, provide description.

a. Need to set NSHealthShareUsageDescription for data reading.

b. Need to set NSHealthUpdateUsageDescription for data writing.

c. Enable capabilities for HealthKit. Select Project-Target > Capabilities > HealthKit.

Enable capabilities for HealthKit
  • This will add an entitlement file automatically in project.
Entitlements file

3. Create Shared Manager (HealthKitManager)

Let’s create a shared class as Manager in which we’ll put our code.


4. Get Write Permissions

See the function below, which will ask for permissions of writing Steps.

& declare function in header file. i.e. HealthKitManager.h

Also import HealthKitManager.h file in ViewController.m

Additional UI,

  • Add a Connect button in View Controller & attach with an action.
Connect button
  • Bind action with Connect button,

All is setup now, run the project… fingers are crossed !!!!

What would you see?: The Connect action.

What the action would do?: A modal will be presented which will ask for permission to write Steps,

Permissions to write Steps

At the end of this step, your application is now authorized to write Steps to the Health application.

Please feel free to ask your queries, if you get any error/problem.


5. Write Data to Health Application

  • Let’s create a function which will write a number of Steps into the Health application. (in HealthKitManager.m)
  • Create action and write Steps from ViewController,
  • As a result you would see your written data (Steps) in Health application.
  • Go to Health application, select tab: Health Data or Today

6. Get Read Permissions

Remember, we already have permission for writing Steps that means we can also read them. So for a new activity type, we’ll get data reading permission for Cycling Distance from Health app.

  • Let’s add button & call permission method from ViewController,
add action: read permission
  • On action of Read Permission, you would be able to see permission modal as follows.
  • We are now authorized to read Cycling Distance data from the Health application.

7. Read Data from Health Application

  • We’ll read Cycling Distance from Health application, but there is not data for Cycling Distance in Health app yet.
  • Let’s add some dummy values manually,
  • Go to Health App > Selection Health Data Tab > Activity > Cycling Distance > Click ‘+’ button > Set Date, Time & miles, then save.
  • Let’s read Cycling distance for May 8th 2018. For this, we will have date function which would help us to convert date-string into the Date object.
  • The above function will collect all the Cycling Distance data entries of the day & will SUM (make a total) of all entries. In this way, we would get the Cycling Distance data of 8th May.
  • Let’s call this function with action in ViewController,

Let’s bind this value with label on screen.


8. Keep things in mind.

  • You can also get Authorization Status of specific type. [Note: This will work only when you work with writing permissions]
  • You should ask for permissions only if NotDetermined yet. [Check auth-status]
  • Permission modal will only appear when you are asking the specific permission for the first time.
  • All data fetching/writing tasks are performed in background thread only [managed by Health application].

Please feel free to drop notes regarding your query and/or your valuable suggestions.

Find complete project on Github.