HKVisionPrescription
A sample that stores a vision prescription.
Declaration
class HKVisionPrescriptionOverview
Use this class to create an image-only prescription. Here, you attach the prescription as an image or PDF to a simple sample. The sample contains only basic information about the prescription, such as the issue and expiration dates. To see the prescription data, people must view the attached image or PDF.
To create an image-only prescription, start by creating an HKVisionPrescription sample object.
// Create a minimal prescription sample that just holds an image attachment.
let prescription = HKVisionPrescription(type: .glasses,
dateIssued: Date(),
expirationDate: nil,
device: HKDevice.local(),
metadata: nil)Next, save the sample to the HealthKit store.
// Save the sample to the HealthKit store.
do {
try await store.save(prescription)
} catch {
// Handle the error here.
fatalError("*** An error occurred while saving the prescription sample to the HealthKit store \(error.localizedDescription) ***")
}Then, you can attach the image or PDF to the sample.
// Get the attachment store.
let attachmentStore = HKAttachmentStore(healthStore: store)
// Attach the image to the sample.
do {
_ = try await attachmentStore.addAttachment(to: prescription,
name: "Glasses Prescription",
contentType: type,
url: url)
} catch {
// Handle the error.
fatalError("*** An error occurred while attaching the image: \(error.localizedDescription) ***")
}For more information about adding images or pdfs as attachments, see HKAttachmentStore. To create a vision prescription sample that contains the full data for the prescription, use HKGlassesPrescription or HKContactsPrescription instead.