Home

Awesome

<!-- This source file is part of the Stanford Spezi open-source project. SPDX-FileCopyrightText: 2022 Stanford University and the project authors (see CONTRIBUTORS.md) SPDX-License-Identifier: MIT -->

Spezi HealthKit

Build and Test codecov DOI

Simplifies access to HealthKit samples ranging from single, anchored, and background queries.

Overview

The Spezi HealthKit module simplifies access to HealthKit samples ranging from single, anchored, and background queries.

Setup

You need to add the Spezi HealthKit Swift package to your app in Xcode or Swift package.

Important: If your application is not yet configured to use Spezi, follow the Spezi setup article and set up the core Spezi infrastructure.

Example

Before you configure the HealthKit module, make sure your Standard in your Spezi Application conforms to the HealthKitConstraint protocol to receive HealthKit data. The add(sample:) function is triggered once for every newly collected HealthKit sample, and the remove(sample:) function

actor ExampleStandard: Standard, HealthKitConstraint {
    // Add the newly collected HKSample to your application.
    func add(sample: HKSample) async {
        // ...
    }

    // Remove the deleted HKSample from your application.
    func remove(sample: HKDeletedObject) {
        // ...
    }
}

Then, you can configure the HealthKit module in the configuration section of your SpeziAppDelegate. Provide HealthKitDataSourceDescription to define the data collection. You can, e.g., use CollectSample to collect a wide variety of HKSampleTypes:

class ExampleAppDelegate: SpeziAppDelegate {
    override var configuration: Configuration {
        Configuration(standard: ExampleStandard()) {
            if HKHealthStore.isHealthDataAvailable() {
                HealthKit {
                    CollectSample(
                        HKQuantityType.electrocardiogramType(),
                        deliverySetting: .background(.manual)
                    )
                    CollectSample(
                        HKQuantityType(.stepCount),
                        deliverySetting: .background(.automatic)
                    )
                    CollectSample(
                        HKQuantityType(.pushCount),
                        deliverySetting: .anchorQuery(.manual)
                    )
                    CollectSample(
                        HKQuantityType(.activeEnergyBurned),
                        deliverySetting: .anchorQuery(.automatic)
                    )
                    CollectSample(
                        HKQuantityType(.restingHeartRate),
                        deliverySetting: .manual()
                    )
                }
            }
        }
    }
}

For more information, please refer to the API documentation.

The Spezi Template Application

The Spezi Template Application provides a great starting point and example using the SpeziHealthKit module.

Contributing

Contributions to this project are welcome. Please make sure to read the contribution guidelines and the contributor covenant code of conduct first.

License

This project is licensed under the MIT License. See Licenses for more information.

Spezi Footer Spezi Footer