iOS (Swift)
SDK Installation
RealTime streaming on iOS is only available to those registered on the Apple Developer Program
Summary:
Add https://github.com/tryterra/TerraRTiOS as a package dependency
In your info.plist, add:
Privacy - Bluetooth Always Usage Description: Justification for BLE usage (shown to the user when requesting permission)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
Error while loading OpenAPI operation — Failed to fetch OpenAPI file
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
Create a WatchOS target within your iOS project:
File -> New -> Target -> Watch App for iOS App.
Enable HealthKit and Background Modes for the WatchOS app, and add the following privacy keys to your
Info.plist:Privacy - Health Share Usage DescriptionPrivacy - Health Update Usage DescriptionPrivacy - 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?