For the complete documentation index, see llms.txt. This page is also available as Markdown.

iOS (Swift)

SDK Installation

Summary:

  1. Add https://github.com/tryterra/TerraRTiOS as a package dependency

  2. In your info.plist, add:

    1. Privacy - Bluetooth Always Usage Description: Justification for BLE usage (shown to the user when requesting permission)

    2. Privacy - Motion Usage Description: Justification for phone motion sensor usage (shown to the user when requesting permission - only add if using phone sensors for streaming)

When the app is launched for the first time, these permissions will be requested from the user.


Connection setup/management

The SDK revolves around the TerraRT class, which manages Bluetooth connections and real-time data streaming.

SDK Initialization

Always initialize the TerraRT class to begin using the SDK.

Do so every time the app is opened or brought into the foreground.

Initializing a Connection

Register the phone by calling initConnection.

This connects the phone to Terra, allows you to use other SDK functions, and allows it to be a producer once a wearable is connected later on.

To generate the token, make the below call from your backend

Generate a mobile SDK auth token

post

Creates a token to be used with initConnection() functions in the Terra mobile SDKs in order to create a user record for Apple Health or Samsung Health (or equivalent)

Authorizations
x-api-keystringRequired

Your API key for authentication

dev-idstringRequired

Your developer ID for authentication and tracking

Responses
200

200

application/json
statusstringOptionalExample: success
tokenstringOptionalExample: 250c68b9c21b78e40e7a3285a2d538d3bc24aabd3b4c76a782fb0a571ca4501d
expires_inintegerOptionalDefault: 0Example: 180
post/auth/generateAuthToken
POST /api/v2/auth/generateAuthToken HTTP/1.1
Host: access.tryterra.co
x-api-key: YOUR_API_KEY
dev-id: YOUR_API_KEY
Accept: */*
{
  "status": "success",
  "token": "250c68b9c21b78e40e7a3285a2d538d3bc24aabd3b4c76a782fb0a571ca4501d",
  "expires_in": 180
}

Connecting a device

iOS devices support connection via BLE or Apple Watch's WatchConnectivity protocol. For Apple Watch, refer to the WatchOS integration guide below.

The startBluetoothScan function returns a SwiftUI TerraBLEWidget view for device selection:


Streaming Real-Time Data

Once the phone is registered and you've connected a wearable, you can stream data from the wearable to your mobile app.

Start Streaming

Call startRealtime to stream data to your app locally (without sending to Terra's server):

To also send data to Terra's server, see the Your App → Terra guide.

Stop streaming and disconnect


WatchOS Integration

To receive a data stream from an Apple Watch, the watch must be instructed to initiate a data stream to the iOS phone it is paired with.

For that, you'll need to have a WatchOS companion app running on the watch, which runs startStream or startExercise

Setup

  1. Create a WatchOS target within your iOS project:

    File -> New -> Target -> Watch App for iOS App.

  2. Enable HealthKit and Background Modes for the WatchOS app, and add the following privacy keys to your Info.plist:

    1. Privacy - Health Share Usage Description

    2. Privacy - Health Update Usage Description

    3. Privacy - Health Records Usage Description

Start Streaming from WatchOS

Outside of a workout session, data recording frequency may be reduced. In order to capture data at the highest possible frequency, you'll want to start a workout session

On the iOS app, listen for updates from the WatchOS app using startRealtime with .WATCH_OS:


Streaming Workouts from WatchOS

The Apple Watch app can also manage a workout session, during which recording frequency is enhanced.

From the Watch: startExercise, stopExercise, pauseExercise, resumeExercise

From the iOS app: pauseWatchOSWorkout, resumeWatchOSWorkout, stopWatchOSWorkout

Last updated

Was this helpful?