Apple HealthKit
Apple HealthKit is an SDK-based provider. Your iOS consumer app would embed the Vital Mobile SDKs on a supported stack. Data are then pulled from the Apple HealthKit data store on the user’s iOS device.
Refer to the Mobile SDK Installation and Vital Health SDK guides for SDK installation instructions and general API usage. This guide contains information on the the behaviour and required configuration specific to the Apple HealthKit integration.
Mobile SDK Installation
Learn about the minimum runtime and build-time requirements of Vital Mobile SDKs, and how to add them into your project through your package dependency manager.
Vital Health SDK
Learn about the initial setup required by Vital Health SDK, and the API exposed by Vital Health SDK for managing and inspecting health data permissions and automatic data sync.
Sync Frequency
App state | Behaviour |
---|---|
Foreground | Apple HealthKit delivers any buffered and new changes immediately. |
Background | Hourly batch delivery of changes, subject to operating system throttling. |
While the SDK schedules hourly data sync with Apple HealthKit, iOS has full discretion to defer the scheduled time based on factors like CPU usage, battery usage, connectivity, and Low Power Mode.
In other words, the Vital SDK — or any third-party HealthKit apps — cannot guarantee the sync to happen on a strict schedule. The hourly schedule is advisory and non-binding from the persective of iOS. The actual delivery frequency can therefore fluctuate from hourly, to once a day, or when certain opportunity arises (e.g., when Sleep Mode ends, or when the phone starts charging).
Getting Started
To enable this integration, you would need to integrate Vital Core SDK and Vital Health SDK into your Native iOS, React Native or Flutter mobile app.
Add Vital SDKs as dependencies
Integrate with Vital Mobile SDK Authentication
Follow the SDK Authentication guidance to integrate your app with the authentication mechanism of Vital Core SDK.
Configure Health SDK and ask user for read permissions
Follow the Vital Health SDK guidance to configure your App Delegate, configure the Health SDK and to ask for health data read permissions from your user.
Health data sync is activated only on VitalResource
s which you have asked permissions from your user for. If you
did not ask for permissions, data sync would not occur.
Configure Background Delivery
Follow the Apple HealthKit Background Delivery guidance below to setup your iOS app target as an eligible target for Apple HealthKit Background Delivery.
Setting up Apple HealthKit Background Delivery
You MUST configure your App Delegate for Apple HealthKit Background Delivery to function as expected.
Vital Health SDK can subscribe to Apple HealthKit Background Delivery, so that data sync can happen automatically in background. The subscription persists even when:
- Your app user does not open your app regularly;
- Your app user forces quit your app; or
- The iPhone has been restarted for any reason.
Enabling Apple HealthKit will attract additional scrutiny in the App Store Review process. You should be prepared to explain and demonstrate your usage of these health and fitness data.
1. Setup app entitlements
Enable the “HealthKit > Background Delivery” entitlement:
Enable the “Background Modes > Background Processing entitlement:
2. Update your Info.plist
In your “Info > Custom iOS Target Properties” section — also known as the Info.plist
file — these entries should be
configured:
Key | Value |
---|---|
Privacy - Health Share Usage Description ( NSHealthShareUsageDescription ) | An explanation of your usage of the user’s HealthKit data. |
Permitted background task scheduler identifiers ( BGTaskSchedulerPermittedIdentifiers ) | Include io.tryvital.VitalHealthKit.ProcessingTask in the array, alongside any other BGTask identifiers of yours. |
If you request write permissions, you must also specify:
Key | Value |
---|---|
Privacy - Health Update Usage Description ( NSHealthUpdateUsageDescription ) | An explanation of what user health data your app is writing to Apple HealthKit. |
3. Epilogue
You are all set!
Note that there is no need to call syncData()
manually at all. Once you have asked the user for permission on
resources, sync would automatically starts every app launch, as well as whenever your app is woken up by Apple HealthKit to
respond to newly available data.
As per the documentation:
HealthKit wakes your app whenever a process saves or deletes samples of the specified type. The system wakes your app at most once per time period defined by the specified frequency. Some sample types have a maximum frequency of
HKUpdateFrequency.hourly
. The system enforces this frequency transparently.For example, on iOS,
stepCount
samples have an hourly maximum frequency.
This means that although we have background delivery’s frequency set to .hourly
, we cannot guarantee hourly syncing on the dot.