Using Apple Health in the Flutter Wrapper

Flutter ==> Apple Health module

The Flutter Inform SDK Wrapper offers modules which you can implement into your mobile project to power data syncing with the Apple Health health data aggregation platform.

The Flutter Inform SDK Wrapper supports Healthkit for iOS and iPadOS. This document lays out the installation and use of the Apple Health integration.

Apple Health allows health and fitness apps to store and share the same on-device data within a unified ecosystem. It also offers a single place for users to control which apps can read and write health and fitness data.

Apple Health combines data from a variety of fitness and health apps along with data captured by Apple Watches (watchOS).


Installation

Apple Health has its own module in the Flutter Wrapper, therefore the following modules are required to use the Apple Health integration:

Installation instructions are detailed in Installation.


Session

A valid session is required in order to use the Apple Health integration in the Validic Inform SDK. See Session for more information.

Record types

The record types used in the Validic Inform Mobile SDK are described in Record Types.

Record Identifiers

More information on record identifiers can be found in Record Identifiers.

Listening for InformRecord Upload

More information on record events can be found in Record Events.


Apple Health


This project integrates Apple Health with the Flutter Inform SDK Wrapper to passively collect data on behalf of an end user. Validic ensures that our Mobile SDK makes use of the minimal scopes needed for our supported data types.


Availability

To use HealthKit in your app, you need to enable the HealthKit capability in your Xcode project:

  1. Open your app's Xcode project.
  2. Select your app target and go to the Signing & Capabilities tab.
  3. Click the + Capability button and add HealthKit.
  4. Ensure that the HealthKit Background Delivery option is also checked if you want your app to receive updates in the background.

Checking HealthKit Availability

Before interacting with HealthKit, check if it is available on the current device:

bool isAvailable = await ValidicHealthKit.isHealthKitAvailable();
if (!isAvailable) {
  print('HealthKit is not available on this device.');
}

Subscriptions

You can retrieve and set HealthKit sample type subscriptions. Calling setSubscriptions will replace any existing subscriptions.

Retrieve Current Subscriptions

List<HealthKitSampleType> subscriptions = await ValidicHealthKit.getSubscriptions();
print('Current subscriptions: $subscriptions');

Set Subscriptions

await ValidicHealthKit.setSubscriptions([HealthKitSampleType.hkQuantityTypeIdentifierStepCount, HealthKitSampleType.hkQuantityTypeIdentifierHeartRate]);
print('Subscriptions updated.');

Fetching Historical Data

Fetch up to 180 days of historical data from HealthKit.

try {
  Map<String, int> history = await ValidicHealthKit.fetchHistory(
    [HistoricalFetchType.summary, HistoricalFetchType.workout],
    {'startDate': DateTime.now().subtract(Duration(days: 7)), 'endDate': DateTime.now()}
  );
  print('Fetched historical data: $history');
} catch (e) {
  print('Error fetching historical data: $e');
}

Apple Health Events

You can receive events when new records are collected from HealthKit.

ValidicHealthKit.onRecordsCollected.listen((event) {
  print('New records collected: $event');
  // example $event: `{recordType: summary, count: 1}`
});