If you haven’t already, visit the Apple Health docs and follow the “Enable HealthKit” section to configure HealthKit.

Installation

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.

Now, let’s setup the MetriportSDK package to receive background updates.

If you are using Objective-C for your AppDelegate file you will need to set it up as follows:

#import "AppDelegate.h"
#import "MetriportConfiguration.h" // add this line

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   [MetriportConfiguration checkBackgroundUpdates]; // add this line

   // your code
}

If you are using Swift for your AppDelegate file you will need to set it up as follows:

import Foundation
import UIKit
import MetriportSDK // add this line
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var bridge: RCTBridge!
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    MetriportClient.checkBackgroundUpdates() // add this line

    // your code
  }
}

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);

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()
    }
}

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.

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.

Apple Health

For details about receiving Apple Health data via webhooks, see the guide here.