Using Apple Health in the Cordova Wrapper

Cordova ==> Apple Health module

The Cordova 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 Cordova 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 Cordova 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 Cordova 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

Not all devices support HealthKit (e.g. iPads running iOS 16 or lower and other platforms such as Android). To check if HealthKit is available, Apple provides an API: [HKHealthStore isHealthDataAvailable]. For convenience, Validic provides a Cordova method to check this value: ValidicMobile.InformHealthKit.isHealthKitAvailable(). Check if HealthKit is available on the device before attempting to access health data:

const healthKitAvailable = await ValidicMobile.InformHealthKit.isHealthKitAvailable();
if (!healthKitAvailable) {
    console.log('HealthKit is not available on this device.');
    return;
}

// Do HealthKit stuff here

Sample Types

Supported Sample types are available in the ValidicMobile.InformHealthKit.SampleType object.

Subscriptions

Set Subscriptions

To subscribe to HealthKit sample types, pass an array of ValidicMobile.InformHealthKit.SampleType values (which are HealthKit sample type identifier strings) to setSubscriptions:

// Subscribe to body mass and steps
const sampleTypes = [ValidicMobile.InformHealthKit.SampleType.HKQuantityTypeIdentifierBodyMass, ValidicMobile.InformHealthKit.SampleType.HKQuantityTypeIdentifierStepCount]
await ValidicMobile.InformHealthKit.setSubscriptions(sampleTypes);

Calling this function a second time will replace any existing subscriptions with the new sampleTypes list.

To remove all subscriptions, pass an empty array:

await ValidicMobile.InformHealthKit.setSubscriptions([]);

Note: Calling ValidicMobile.InformCore.endSession(); will remove all HealthKit subscriptions and stop listening for new data.

Fetching Historical Data

The Validic Mobile library provides the ability to query up to 180 days of data for a subset of data types provided by HealthKit.

Use fetchHistoryRecords to fetch historical HealthKit data and receive full InformRecord objects. Pass an array of SampleType values under the sampleTypes key, with optional from and to dates. Use getSampleTypesForRecordType to look up the sample types for a given Inform record type.

const sampleTypes = await ValidicMobile.InformHealthKit.getSampleTypesForRecordType(ValidicMobile.InformCore.InformRecordType.Summary);

const start = new Date();
start.setDate(start.getDate() - 29);
const end = new Date();

const result = await ValidicMobile.InformHealthKit.fetchHistoryRecords({
    sampleTypes: sampleTypes,
    from: start,
    to: end
});

console.log(result.informRecords); // Array of InformRecord objects

To limit the fetch to only subscribed sample types (avoiding a HealthKit permissions prompt for unsubscribed types):

// Get user's current subscriptions
const subscriptions = await ValidicMobile.InformHealthKit.getSubscriptions();
// Get all Sample Types included in Summary records
const summarySampleTypes = await ValidicMobile.InformHealthKit.getSampleTypesForRecordType(ValidicMobile.InformCore.InformRecordType.Summary);
// Make a list of all subscribed Summary sample types
const subscribedSummarySampleTypes = subscriptions.filter(type => summarySampleTypes.includes(type));

const start = new Date();
start.setDate(start.getDate() - 29);
const end = new Date();

const result = await ValidicMobile.InformHealthKit.fetchHistoryRecords({
    sampleTypes: subscribedSummarySampleTypes,
    from: start,
    to: end
});

Apple Health Events

Listen for SubscriptionRecordsEvent to receive full InformRecord objects when HealthKit subscription records are collected:

document.addEventListener(ValidicMobile.InformHealthKit.SubscriptionRecordsEvent, (event) => {
    console.log(event.payload.informRecords); // Array of InformRecord objects
});