HKVerifiableClinicalRecordQuery
A query for one-time access to a SMART Health Card or EU Digital COVID Certificate.
Declaration
class HKVerifiableClinicalRecordQueryOverview
Use an HKVerifiableClinicalRecordQuery object to request one-time access to a SMART Health Card or EU Digital COVID Certificate. For example, the following code requests cards that represent immunizations within the last six months.
// Calculate the start and end dates.
var sixMonthsAgo = DateComponents()
sixMonthsAgo.month = -6
let end = Date()
guard let start = Calendar.current.date(byAdding: sixMonthsAgo, to: end) else {
fatalError("*** Unable to calculate a date using \(end) and \(sixMonthsAgo) ***")
}
// Create the predicate.
let lastSixMonthsPredicate =
HKQuery.predicateForVerifiableClinicalRecords(withRelevantDateWithin: DateInterval(start: start, end: end))
// Create the query for immunization cards.
let query = HKVerifiableClinicalRecordQuery(
recordTypes: [HKVerifiableClinicalRecordCredentialType.covid19.rawValue,
HKVerifiableClinicalRecordCredentialType.immunization.rawValue],
sourceTypes: [.smartHealthCard, .euDigitalCOVIDCertificate],
predicate: lastSixMonthsPredicate) { query, records, error in
if let error = error {
// Handle errors here.
fatalError("*** An error occurred: \(error.localizedDescription)***")
}
if let records = records {
// Use the records here.
for record in records {
print(record.recordTypes)
print(record.itemNames)
print(record.issuedDate)
print(record.expirationDate ?? "No expiration date.")
}
}
}
// Run the query.
store.execute(query)Unlike other HealthKit queries, you don’t need to request permission to read verifiable health records before running this query. HealthKit prompts the user for permission to read the records each time you run the query.