1. More Info
  2. Apple Health

Overview

This is a guide to help you get started with iOS, specifically Apple Health. First and foremost Apple Health is a central repository for health and fitness data only available on iPhone. Where Metriport comes in is we provide an SDK that allows you to be able to receive Apple Health data in the background. When data gets added to Apple Health by a user it will simply send that data to the Metriport api and from there it gets sent to your webhook URL.

Apple Health is available for devices on iOS v13 or higher. This also applies for when developing on a simulator.

As a prerequisite we suggest looking at our webhooks docs to understand how the flow works.

Initial Setup

1. Enable HealthKit

First thing you need to do is enable HealthKit capabilities for your app. In Xcode, select the project, click on the Signing & Capabilities tab and add the HealthKit capability. Make sure to select Background Delivery under Capabilities to be able to receive data via webhooks.

You will need a paid Apple Developer account to be able to access HealthKit capabilities. Otherwise, you will see provisioning errors when trying to build the project, and won’t be able to integrate with Apple Health.

If you have an Info.plist file in your project directory, add the following key to the file:

<key>NSHealthShareUsageDescription</key>
<string>Share health usage with this app.</string>

If you don’t have an Info.plist file, you can set this by select the Xcode project, clicking on the Info tab, and adding the key/value there.

2. Install Metriport iOS SDK Package

Our packages can be downloaded using SPM (Swift Package Manager). To add a package dependency to your Xcode project, select File > Add Packages and enter our repo URL:

https://github.com/metriport/metriport-ios-sdk

We recommend using the latest available package release version - you can find the latest release version in our GitHub repo.

Usage

To use our SDK go to the root of your project and add:

import MetriportSDK

var healthStore = MetriportHealthStoreManager(clientApiKey: "CLIENT_API_KEY", sandbox: false);

Ensure you use your Client Key here, and not your Secret Key. You can read more about the differences between the two in our API Keys guide.

As per the guide, we recommend to store your Client Key in environment secrets, and not commit it in plain text to your repository.

Next, initialize the MetriportWidget inside of a view to display it. See the following snippet for an example initialization from a button tap, that then displays the Connect Widget in a sheet:

class WidgetController: ObservableObject {
    @Published var showWidget = false;
    var token = "";

    func openWidget(token: String) {
        self.showWidget = true
        self.token = token
    }

}

struct ContentView: View {
    // Manages the Metriport Widget
    @ObservedObject var widgetController = WidgetController()

    var body: some View {
        VStack {
            Button {
                // This is an example, you'll need to get a session token from your server.
                let token = "TOKEN"
                widgetController.openWidget(token: token);
            } label: {
                Text("Open Metriport Widget")
            }
            .sheet(isPresented: $widgetController.showWidget) {
                MetriportWidget(
                    healthStore: healthStore,
                    token: widgetController.token,
                    sandbox: false)
            }
        }
        .padding()
    }
}

As per the Quickstart guide, you’ll need to generate a token on your server before opening the Connect Widget. You can read more about this here.

Receiving Data

The way we receive and send data via webhooks is in 2 steps:

Step 1: When the user first connects to Apple Health we will make an initial query for all data within the last 30 days. This will be a daily average of all data types.

Step 2: Once this data is queried we will then listen on an hourly basis for any updates to the Apple Health. If there are changes then a query will be sent via webhook with just the data that’s been added.

To understand the data you should expect to receive from Apple Health checkout our data models.