iOS (Swift)
Last updated
Was this helpful?
Last updated
Was this helpful?
First, initialize the TerraManager
class within your ContentView file.
Terra
initializationTerra
should be initialized every time your app is brought into the foreground
This is a necessary prerequisite for other SDK functions to run as expected
To do this, run the Terra manager initialization function as below:
Terra
is thereafter ready to be used with other functions
Once Terra is initialized, you can create an Apple Health connection.
initConnection
only needs to be run a single time. Apple Health prohibits the permission popup to appear more than once for any given permission, so calling initConnection
more than once will result in no action at all
The only case where it would re-appear is if:
you call initConnection with an expanded set of customPermissions
the app is deleted & reinstalled.
🚧 Webviews & HealthKit permissions screen
HealthKit implements the permissions popup as a webview
If your app is also based on a webview, you will need to interrupt your webview, call initConnection
, then upon completion re-open your webview
This will ensure you get updates for the user's Apple Health data automatically sent to your destination.
Data will
still be sent even if your app is exited/killed completely
only be sent if the device is unlocked
Always use this method right after initializing Terra to see if the connection still exists,
if the connection exists, all is good! 🎉 keep vibing along
In order to disconnect an Apple Health user, you may use the same endpoint as for Web API-based integrations, called from your backend
You can request for historical data using one of the data retrieval functions.
You may set toWebhook
to false if you wish for the callback function to return the data payload on the client side.
Check out the iOS SDK reference for details about all the functions in the SDK
You may write other types of data into Apple Health through one of the helper functions in the SDK
device_data
must be passed in for postActivity to succeed.
postActivity
uses the HKWorkout class internally, which has been deprecated as of iOS 17.
Planned Workouts are only available for iOS 17 and above
Writing planned workouts allows you to create a workout plan for the user (such as those commonly used with Zwift)
This will typically look like the following example:
Planned workouts consist of a series of steps. Each step has:
Completion criteria (called duration for all intents and purposes). For example:
distance (keep going until you cover 500m),
energy expenditure (keep going until you burn 200 kcal)
heart rate threshold (keep going until your heart rate reaches above 150 bpm)
etc..
A target to be maintained
heart rate (maintain heart rate between certain bounds)
speed (maintain speed between certain bounds)
grade (inclination above a certain threshold)
etc...
An associated intensity label
Warmup
Active
Cooldown
etc..
An exercise type
Running
Cycling
Deadlifting
etc...
A series of sub-steps
These modalities allow any workout to be described in a series of defined steps for each of its segments.
Once written, a planned workout will be available in the user's Workout app on their Apple Watch. They will then be able to follow the workout and receive guidance throughout each step.
In order to write a workout for the user to follow on their Apple Watch, use the postPlannedWorkout function as below, with the instance of terra you created above
Run the on the terra
instance you created above to have the Apple Health permission screen pop up.
customPermissions
is used to customize the permissions list shown in the Apple Health popup when calling . When empty, it defaults to all available permissions
Add to your AppDelegate's didFinishLaunchingWithOptions
function.
To ensure a connection is still valid on the client side, use the method. This function is synchronous and returns the user_id
right away or nil if none exists.
if you expect the connection to exist, but returns nil, call terra.initConnection to re-establish it. No permission screen will be shown again and the user flow will be unaffected
if it is expected for the connection to no longer exist, you may allow the user to re-connect Apple Health if they so choose (you'd then call as before, but no permission screen would be shown)
token
is a single-use token created to ensure the authentication endpoint for creating a (and connecting the SDK to Terra's servers) does not get abused.
In order to generate it, place the call below on your server, and provide it to your client side using your own logic.
During the development phase, it it acceptable to place this call on the client side, exposing your API key in your mobile app.
For a production implementation, DO NOT expose your API key this way, and make sure to only make this call from your backend
If you need to access Terra's Health & Fitness API, it is only secure to do so from your server backend and send the desired data to your mobile frontend.
Never expose your API key on the client side unless you are simply testing
Add https://github.com/tryterra/TerraiOS
as a package dependency
Add Capabilities:
Healthkit > Healthkit Background Delivery
Background Modes > Background processing
Background modes > Background fetch
In your info.plist, add:
Privacy - Health Share Usage Description
: custom text shown in the Apple Health permission screen
Permitted background task scheduler
: co.tryterra.data.post.request
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)
/auth/generateAuthToken
your developer ID
testingTerra
your API key
OtHJok60oQmT8zhnUWc4SWBJI7ztPTs88C0gOsJJ