The HealthKitOnFHIR library provides extensions that convert supported HealthKit samples to corresponding FHIR resources using FHIRModels encapsulated in a ResourceProxy.
HealthKitOnFHIR can be installed into your Xcode project using Swift Package Manager.
In Xcode 14 and newer (requires Swift 5.7), go to “File” » “Add Packages…”
Enter the URL to this GitHub repository, then select the HealthKitOnFHIR package to install.
Usage
The HealthKitOnFHIR library provides extensions that convert supported HealthKit samples to corresponding FHIR resources using FHIRModels encapsulated in a ResourceProxy.
let sample: HKSample = // ...
let resource = try sample.resource
Observations
HKQuantitySample, HKCategorySample, HKCorrelationSample, and HKElectrocardiogram will be converted into FHIR Observation resources encapsulated in a ResourceProxy.
let sample: HKQuantitySample = // ...
let observation = try sample.resource.get(if: Observation.self)
Codes and units can be customized by passing in a custom HKSampleMapping instance to the resource(withMapping:) method.
let sample: HKQuantitySample = // ...
let sampleMapping: HKSampleMapping = // ...
let observation = try sample.resource(withMapping: sampleMapping).get(if: Observation.self)
Clinical Records
HKClinicalRecord will be converted to FHIR resources based on the type of its underlying data. Only records encoded in FHIR R4 are supported at this time.
let allergyRecord: HKClinicalRecord = // ...
let allergyIntolerance = try allergyRecord.resource.get(if: AllergyIntolerance.self)
Example
In the following example, we will query the HealthKit store for step count data, convert the resulting samples to FHIR observations, and encode them into JSON.
import HealthKitOnFHIR
// Initialize an HKHealthStore instance and request permissions with it
// ...
let date = ISO8601DateFormatter().date(from: "1885-11-11T00:00:00-08:00") ?? .now
let sample = HKQuantitySample(
type: HKQuantityType(.heartRate),
quantity: HKQuantity(unit: HKUnit.count().unitDivided(by: .minute()), doubleValue: 42.0),
start: date,
end: date
)
// Convert the results to FHIR observations
let observation: Observation?
do {
try observation = sample.resource.get(if: Observation.self)
} catch {
// Handle any mapping errors here.
// ...
}
// Encode FHIR observations as JSON
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes, .sortedKeys]
guard let observation,
let data = try? encoder.encode(observation) else {
// Handle any encoding errors here.
// ...
}
// Print the resulting JSON
let json = String(decoding: data, as: UTF8.self)
print(json)
The following example generates the following FHIR observation:
This project is licensed under the MIT License. See Licenses for more information.
Contributors
This project is developed as part of the Stanford Biodesign for Digital Health projects at Stanford.
See CONTRIBUTORS.md for a full list of all HealthKitOnFHIR contributors.
Notices
HealthKit is a registered trademark of Apple, Inc.
FHIR is a registered trademark of Health Level Seven International.
HealthKitOnFHIR
The HealthKitOnFHIR library provides extensions that convert supported HealthKit samples to corresponding FHIR resources using FHIRModels encapsulated in a ResourceProxy.
For more information, please refer to the API documentation.
HealthKitOnFHIR supports:
Please refer to the HKObject Support Table for a complete list of supported types.
Installation
HealthKitOnFHIR can be installed into your Xcode project using Swift Package Manager.
HealthKitOnFHIR
package to install.Usage
The HealthKitOnFHIR library provides extensions that convert supported HealthKit samples to corresponding FHIR resources using FHIRModels encapsulated in a ResourceProxy.
Observations
HKQuantitySample
,HKCategorySample
,HKCorrelationSample
, andHKElectrocardiogram
will be converted into FHIR Observation resources encapsulated in a ResourceProxy.Codes and units can be customized by passing in a custom
HKSampleMapping
instance to theresource(withMapping:)
method.Clinical Records
HKClinicalRecord
will be converted to FHIR resources based on the type of its underlying data. Only records encoded in FHIR R4 are supported at this time.Example
In the following example, we will query the HealthKit store for step count data, convert the resulting samples to FHIR observations, and encode them into JSON.
The following example generates the following FHIR observation:
License
This project is licensed under the MIT License. See Licenses for more information.
Contributors
This project is developed as part of the Stanford Biodesign for Digital Health projects at Stanford. See CONTRIBUTORS.md for a full list of all HealthKitOnFHIR contributors.
Notices
HealthKit is a registered trademark of Apple, Inc. FHIR is a registered trademark of Health Level Seven International.