Q: How to get Apple HealthKit data with the Terra iOS SDK

CostlyQuail1 month ago

How do I get started with the TerraManager in the iOS sdk. We have already followed the guide on enabling HealthKit permissions but the flow does not make sense to me

terra

Chandruadmin1 month ago

Hi there.

After following the guide on setting up the dependencies for the iOS sdk, the flow would look something like this:

  1. Create a manager instance:
var terra: TerraManager Terra.instance(devId: "<YOUR-DEV-ID>", referenceId: "<YOUR-INTERNAL-USER-ID>") { manager, error in if error == nil { terra = manager } }
  1. Generate an auth token. For this, you should implement a backend endpoint to get the token from Terra using your dev-id, x-api-key and then call this endpoint from your mobile app (protected by your auth). For example
@app.get('/terra/sdktoken') def get_terra_sdk_token(): # perform auth for your user if required headers = { "dev-id": "<YOUR-DEV-ID>", "x-api-key": "<YOUR-API-KEY>" } response = requests.post("https://api.tryterra.co/v2/auth/generateAuthToken", headers=headers) data = response.json() return { 'token': data['token'] }

And then request this endpoint from your app and use the token

  1. Then initialise a connection, which should pull up the HealthKit permissions screen (this only needs to be done once):
terra.initConnection(type: .APPLE, token: token, customReadTypes: [], schedulerOn: true) { success, error in if !success { print("Failed to initialise connection") } }
  1. Now request data as desired (ensuring you use swift date objects):
terra.getDaily(type: .APPLE, startDate: Date, endDate: Date, toWebhook: true)