Skip to content

Instantly share code, notes, and snippets.

@NSAmit
Last active March 8, 2019 01:16
Show Gist options
  • Save NSAmit/ebeaed4e1f5f788ba17edcb9140a7848 to your computer and use it in GitHub Desktop.
Save NSAmit/ebeaed4e1f5f788ba17edcb9140a7848 to your computer and use it in GitHub Desktop.
Code to request authorization for Clinical Records
// Make sure you import HealthKit
// Get the HKHealthStore reference. Must be single reference in your app.
let healthStore = HKHealthStore()
// Create required Record Type's equivalent HKClinicalType using clinicalType func of HKObjectType
guard let allergyRecord = HKObjectType.clinicalType(forIdentifier: .allergyRecord),
let medicationRecord = HKObjectType.clinicalType(forIdentifier: .medicationRecord),
let conditionRecord = HKObjectType.clinicalType(forIdentifier: .conditionRecord),
let immunizationRecord = HKObjectType.clinicalType(forIdentifier: .immunizationRecord),
let labResultRecord = HKObjectType.clinicalType(forIdentifier: .labResultRecord),
let procedureRecord = HKObjectType.clinicalType(forIdentifier: .procedureRecord),
let vitalSignRecord = HKObjectType.clinicalType(forIdentifier: .vitalSignRecord) else {
// Handle errors here. This could in case the OS on device is < 12.0. You can use @available(iOS 12.0, *) to avoid that
fatalError("*** Unable to create the requested types ***")
}
// Pass the Set of required HKClinicalType to get authorization for read only. As Clinical Records as Read only.
healthStore.requestAuthorization(toShare: nil, read: [allergyRecord, medicationRecord, conditionRecord, conditionRecord, immunizationRecord, labResultRecord, procedureRecord, vitalSignRecord]) { (success, error) in
guard success else {
// Handle errors here.
fatalError("*** An error occurred while requesting authorization: \(error!.localizedDescription) ***")
}
// Your requested access has been authorized by the user.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment