Provider Lifecycle
What happens when a user connects Apple Health, changes phones, signs out, disconnects, or is deleted.
This page covers Apple Health, through the iOS SDK or the React Native module. Health Connect and Samsung Health, through the Android SDK in early access, follow the same lifecycle.
Connection Model
A connection belongs to one Sonar user and one app installation. Each user has at most one Apple Health connection at a time.
The SDK session and the connection are separate. signOut ends the session on one installation and keeps the connection and its data. disconnect removes the connection and deletes its data.
Connect
Check the Device
The SDK checks that the device has Apple Health. If not, connect rejects with provider_unavailable.
Ask for Permission
The SDK shows the Apple Health sheet for every type in Supported Native Data. The user can allow some types and not others. Apple does not tell the SDK which ones, so the connection goes ahead either way and syncs whatever it can read.
Record the Connection
Sonar records the connection. Your backend receives device.connected when a new connection is created.
Start Syncing
The SDK sends recent days first, then works back through the history period, then keeps up with new data. See SDK Synchronization.
Calling connect again on a connected installation returns already_connected. If the user never answered the Health permission sheet for some types, it first shows the sheet again for those types.
New Phone or Reinstall
The newest installation to connect wins. When the same user connects Apple Health on a new phone, or after reinstalling the app:
- The new installation takes over the existing connection. The data already in Sonar stays. No new
device.connectedis sent. - The new installation imports the history period from its own Health data. Sonar skips the records it already holds.
- The old installation stops syncing the next time it talks to Sonar, and its state shows
disconnected. If the user connects there again, that installation takes the connection back.
Picking the Connection Up
A user who already has Apple Health connected may sign in on an installation that never held their connection: a new phone, a reinstall, or another account signing in on a shared phone. authenticate checks for this, so the user does not stop syncing without noticing:
- If Health access needs no sheet, because the user already allowed it for your app on this phone, the SDK connects on its own and starts syncing.
- Otherwise the state shows
disconnectedwithconnect_provider. Offer the user to connect, and callconnectwhen they agree.
This happens once per installation and user. An installation that lost the connection to a newer phone does not take it back on its own; only an explicit connect does.
Sign Out
signOut revokes this installation’s session and clears its credentials. Sonar keeps the connection and all its data.
Call it only when the user signs out of your app or removes their account. Never call it at launch or before authenticate: at every launch, authenticate alone keeps the session going.
When the same user signs in again on this installation, the SDK picks the sync up where it stopped, with no need to call connect. When a different user signs in, the SDK forgets where the previous user’s sync stopped, and picks up the new user’s connection if their account has one (see Picking the Connection Up). Until someone signs in, the connection stays in Sonar with no phone sending to it, and turns stale after 14 days (see below).
Disconnect
disconnect(provider) deletes data. It is not a pause.
- The SDK stops syncing on this installation.
- Sonar stops accepting data for the connection. It disappears from
GET /v1/users/{id}. - Sonar deletes the provider’s data in the background and recomputes the user’s data without it.
- When the deletion is done, your backend receives
device.disconnected.
disconnect returns disconnecting while Sonar deletes. Calling it again does not start a second deletion. An installation that does not hold the connection gets disconnected and deletes nothing.
The phone’s state stays disconnecting until the app launches again or you call connect; then it shows disconnected. Use the device.disconnected webhook on your backend to know the deletion is complete.
To stop using a provider’s data without deleting it, change the user’s consolidation settings from your backend instead.
Disconnect from Your Backend
If the user lost the phone or removed the app, your backend can disconnect with its API key:
DELETE /v1/users/{id}/devices/apple_healthIt returns 204, and Sonar deletes the data the same way. The phone, if it is still around, stops syncing the next time it talks to Sonar and shows disconnected. This does not revoke the installation’s SDK session. Revoke that separately; see SDK Authentication.
Reconnect
If the deletion is still running, connect returns pending_cleanup and the state stays connecting. Call connect again later. The SDK also tries once on the next app launch.
Once the deletion is done, connect starts a new connection and a full import: recent days first, then the history period.
resync(provider) is different. It sends the data again without deleting anything. To rebuild from scratch, disconnect, wait for device.disconnected, then connect.
Delete the User
When your backend deletes the user, Sonar ends every SDK session for that user and deletes all their data. The phone learns it the next time it talks to Sonar: the SDK clears its data on the phone, and its state returns to signed_out. A call that runs into this rejects with user_deleted.
What Your Backend Sees
GET /v1/users/{id} lists the connection with provider: "apple_health", connected_at, last_sync_at, and last_seen_at. Atlas shows the same under Developers → Users.
status is connected, or stale when the phone has not checked in or sent data for 14 days. last_sync_at is the last time new data arrived from the phone. last_seen_at is the last time the phone checked in. The SDK checks in even when it has nothing new, so stale usually means the app was removed from the phone or the user signed out. Sonar keeps a stale connection and its data. Disconnect it to remove both.
| Event | Connection | Data in Sonar | SDK session |
|---|---|---|---|
| User allows only some types | Connected | Allowed types sync | Unchanged |
| User turns off Health access later | Connected: the phone still checks in, but last_sync_at stops moving. iOS does not tell apps about read access |
Kept | Unchanged |
signOut |
Kept | Kept | Revoked on that installation |
| App removed from the phone | Kept, then stale after 14 days |
Kept | Expires unused |
| Same user connects on another installation | Moves to the new installation | Kept | Unchanged |
disconnect or backend DELETE |
Removed | Deleted | Unchanged |
| User deleted | Removed | Deleted | All revoked |
Turning off permission never deletes data. Only a disconnect or a user deletion does.
Sonar