iOS (Swift)

SDK Installation

RealTime streaming on iOS is only available to those registered on the Apple Developer Program

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.

To do this, run the TerraRT manager initialization function as below:

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.

Use the terraRT instance created above and call initConnection as below:

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

Connecting a device

iOS devices only support connection via BLE or Apple Watch's custom bluetooth protocol. For the Apple Watch connection, refer to the guide below

You may use an example as below to call the startBluetoothScan function, which pulls up a modal allowing the user to select a wearable to connect


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

You can start receiving data from the connected wearable's stream by calling startRealtime with the following signature

you can also stop the stream and disconnect the wearable once done


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

To begin streaming data from Apple Watch, call the startStream() function from within the Watch app. This will initiate data streaming from the Watch sensors to the paired iOS app.

On the iOS app, you'll equivalently need to listen for updates from the WatchOS app:


Streaming Workouts from WatchOS

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

To manage a workout session, you can use the following convenience functions:

From the Watch:

  • startExercise

  • stopExercise

  • resumeExercise

  • pauseExercise

From the iOS app:

  • pauseWatchOSWorkout

  • resumeWatchOSWorkout

  • stopWatchOSWorkout

Here is an example template of the watchOS app using the functions outlined above

Last updated

Was this helpful?