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

iOS (Swift)

The iOS RT SDK has two separate APIs: one for the iOS companion app (TerraRT class) and one for watchOS (Terra class). They communicate via WatchConnectivity.

Connections

public enum Connections: String {
    case BLE        // Bluetooth Low Energy devices (heart rate monitors, etc.)
    case APPLE      // iPhone's built-in sensors
    case WATCH_OS   // Data from paired Apple Watch via WatchConnectivity
}

DataTypes

public enum DataTypes: String {
    case HEART_RATE
    case ECG
    case STEPS
    case HRV
    case CALORIES
    case LOCATION
    case SPEED
    case DISTANCE
    case STEPS_CADENCE
    case FLOORS_CLIMBED
    case GYROSCOPE
    case ACCELERATION
    case CORE_TEMPERATURE
    case SKIN_TEMPERATURE
}

Update

Data points delivered via streaming callbacks.

For scalar metrics (heart rate, steps, calories), the value is in val. For vector metrics (acceleration, gyroscope, location), the values are in d.

Device

Represents a discovered BLE device.

ConnectionState

TerraError


iOS Functions (TerraRT class)

Initialization

TerraRT

Creates and authenticates a TerraRT instance. Makes a network call to Terra's servers.

  • devId ➡ Your developer ID from the Terra Dashboard.

  • referenceId ➡ Optional user identifier in your system. Appears as reference_id in webhooks.

  • completiontrue if initialization succeeded, false on failure.

Connection Setup

initConnection

Authenticates the SDK user with Terra's backend. Makes a network call. The token should be generated server-side via the Generate Authentication Token endpoint.

getUserid

Returns the Terra user ID, or nil if not yet initialized. Synchronous, no network call.

disconnect

Disconnects from the given connection type.

Device Scanning

startBluetoothScan (with built-in UI)

Starts scanning and returns a SwiftUI TerraBLEWidget view for device selection. When bluetoothLowEnergyFromCache is true, attempts to reconnect to a previously cached device.

startBluetoothScan (with device callback)

Starts scanning and calls deviceCallback for each discovered device, giving you programmatic control over device selection.

stopBluetoothScan

Stops an active Bluetooth scan.

connectDevice

Connects to a specific Device returned from startBluetoothScan(deviceCallback:).

getConnectedDevice

Returns the currently connected BLE device, or nil.

Data Streaming

Three startRealtime overloads for different streaming modes:

startRealtime (server + local)

Streams data to both Terra's websocket server (via token) and a local callback.

startRealtime (server only)

Streams data to Terra's websocket server only. No local callback — data is not delivered to the app.

startRealtime (local only)

Streams data to a local callback only. No server connection — data stays on the device.

Parameters:

  • type ➡ The connection type (.BLE, .APPLE, .WATCH_OS).

  • dataType ➡ Set of data types to stream.

  • token ➡ The producer token for the websocket connection to Terra, minted from POST /auth/usernot the initConnection token. See Authentication tokens.

  • callback ➡ Called with each Update data point.

  • connectionCallback ➡ Called when the websocket connection state changes.

stopRealtime

Stops streaming for a given connection type.

Websocket

setWebsocketListener

Registers a listener for websocket connection state changes.

  • Bool indicates connected/disconnected, Error? provides failure details.

iOS → WatchOS Functions

These functions control the Apple Watch companion app via WatchConnectivity.

These require a watchOS app using the watchOS functions below.

connectWithWatchOS

Establishes a WatchConnectivity session with the paired Apple Watch.

Throws TerraError.FeatureNotSupported if WatchConnectivity is not available.

setWatchOSConnectionStateListener

Fires when the WatchConnectivity session state changes.

setWatchStateChangeListeners

Registers listeners for watch pairing state changes.

pauseWatchOSWorkout / resumeWatchOSWorkout / stopWatchOSWorkout

Controls a workout running on the watchOS companion.

sendMessage / setMessageHandler

Send and receive arbitrary messages between iOS and watchOS.


WatchOS Functions (Terra class)

The watchOS Terra class runs on the Apple Watch. It manages workouts, sensor streaming, and communication with the iOS companion app.

Initialization

No parameters. Throws if HealthKit is not available.

All methods below are called on the Terra instance.

ReadTypes

watchOS uses ReadTypes instead of DataTypes:

WorkoutTypes

81 workout types available — maps to HealthKit HKWorkoutActivityType. Common values: RUNNING, WALKING, BIKING, SWIMMING_POOL, SWIMMING_OPEN_WATER, HIKING, YOGA, STRENGTH_TRAINING, HIGH_INTENSITY_INTERVAL_TRAINING, etc.

WorkoutStates

Connection

connect

Establishes a WatchConnectivity session with the iOS companion app.

sharingDataAuthorised

Returns whether HealthKit sharing authorization has been granted.

setWatchOSConnectionStateListener

Data Streaming

startStream

Starts streaming sensor data from the watch. Data is delivered via the update handler.

stopStream

setUpdateHandler

Registers a callback for incoming data updates.

Workout Session Management

startExercise

Starts an HKWorkoutSession, enabling enhanced sensor access.

pauseExercise / resumeExercise

stopExercise

setWorkoutStateListener

Fires when the workout session state changes.

Messaging

Last updated

Was this helpful?