stanfordspezi/spezihealthkit
This source file is part of the Stanford Spezi open-source project.
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/handleNewSamples(:ofType:)) function is called once for every batch of newly collected HealthKit samples, and the HealthKitConstraint/handleDeletedObjects(:ofType:)) function is called once for every batch of deleted HealthKit objects.
actor ExampleStandard: Standard, HealthKitConstraint {
// Add the newly collected HealthKit samples to your application.
func handleNewSamples<Sample>(
_ addedSamples: some Collection<Sample> & Sendable,
ofType sampleType: SampleType<Sample>
) async {
// ...
}
// Remove the deleted HealthKit objects from your application.
func handleDeletedObjects<Sample>(
_ deletedObjects: some Collection<HKDeletedObject> & Sendable,
ofType sampleType: SampleType<Sample>
) async {
// ...
}
}Then, you can configure the HealthKit module in the configuration section of your SpeziAppDelegate. You can, e.g., use CollectSamples to collect a wide variety of HealthKit data types:
class ExampleAppDelegate: SpeziAppDelegate {
override var configuration: Configuration {
Configuration(standard: ExampleStandard()) {
HealthKit {
CollectSamples(.activeEnergyBurned)
CollectSamples(.stepCount, start: .manual)
CollectSamples(.pushCount, start: .manual)
CollectSamples(.heartRate, continueInBackground: true)
CollectSamples(.electrocardiogram, start: .manual)
RequestReadAccess(quantity: [.bloodOxygen])
}
}
}
}[!TIP] See
SampleTypefor 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]
Package Metadata
Repository: stanfordspezi/spezihealthkit
Default branch: main
README: README.md