Skip to content

Latest commit

 

History

History
125 lines (89 loc) · 6.6 KB

README.md

File metadata and controls

125 lines (89 loc) · 6.6 KB

Spezi HealthKit

Build and Test codecov DOI

Access Health data in your Spezi app.

Overview

The Spezi HealthKit module enables apps to integrate with Apple's HealthKit system, fetch data, set up long-lived background data collection, and visualize Health-related data.

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 to set up the core Spezi infrastructure.

Health Data Collection

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

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. You can, e.g., use CollectSample to collect a wide variety of HealthKit data types:

class ExampleAppDelegate: SpeziAppDelegate {
    override var configuration: Configuration {
        Configuration(standard: ExampleStandard()) {
            HealthKit {
                CollectSample(.activeEnergyBurned)
                CollectSample(.stepCount, start: .manual)
                CollectSample(.pushCount, start: .manual)
                CollectSample(.heartRate, continueInBackground: true)
                CollectSample(.electrocardiogram, start: .manual)
                RequestReadAccess(quantity: [.bloodOxygen])
            }
        }
    }
}

Tip

See SampleType for a complete list of supported sample types.

Querying Health Data in SwiftUI

You can use SpeziHealthKitUI's HealthKitQuery and HealthKitStatisticsQuery property wrappers to access the Health database in a View:

struct ExampleView: View {
    @HealthKitQuery(.heartRate, timeRange: .today)
    private var heartRateSamples

    var body: some View {
        ForEach(heartRateSamples) { sample in
            // ...
        }
    }
}

Additionally, you can use SpeziHealthKitUI's HealthChart to visualise query results:

struct ExampleView: View {
    @HealthKitQuery(.heartRate, timeRange: .today)
    private var heartRateSamples

    var body: some View {
        HealthChart {
            HealthChartEntry($heartRateSamples, drawingConfig: .init(mode: .line, color: .red))
        }
    }
}

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