HKContactsPrescription
A sample that store a prescription for contacts.
Declaration
class HKContactsPrescriptionOverview
To create a sample that stores a contacts prescription, start by defining a specification for each eye. Each lens specification object requires a sphere parameter. This measures the lens’s strength for correcting either nearsightedness or farsightedness (measured in diopter() units).
// The correction for farsightedness.
let sphere = HKQuantity(unit: .diopter(), doubleValue: -0.75)Next, create values for any of the prescription’s optional parameters. For example, if the prescription corrects for astigmatism, create the cylinder and axis values. The cylinder value uses diopter() units, while the axis uses degreeAngle().
// The corrections for astigmatism.
let cylinder = HKQuantity(unit: .diopter(), doubleValue: -0.5)
let axis = HKQuantity(unit: .degreeAngle(), doubleValue: 155.0)To add a multifocal correction for reading, create an addPower value using diopter() units.
// Multifocal correction for reading.
let addPower = HKQuantity(unit: .diopter(), doubleValue: +2.00)To add fitting information for the contact lens, create baseCurve and diameter values. Both of these values use millimeters.
// The fitting information.
let baseCurve = HKQuantity(unit: HKUnit.meterUnit(with: .milli),
doubleValue: 9.0)
let diameter = HKQuantity(unit: HKUnit.meterUnit(with: .milli),
doubleValue: 12.0)Then you can create the HKContactsLensSpecification lens specification.
// The prescription for the right eye.
let contactsRightEye = HKContactsLensSpecification(sphere: sphere,
cylinder: cylinder,
axis: axis,
addPower: addPower,
baseCurve: baseCurve,
diameter: diameter)After you create your lens specifications, you can create an HKContactsPrescription sample.
// The date the doctor issued the prescription.
let dateIssued = Date()
// The date when the prescription expires.
let expirationDate = dateIssued.addingTimeInterval(60 * 24 * 365)
// The contacts prescription.
let prescription = HKContactsPrescription(rightEyeSpecification: contactsRightEye,
leftEyeSpecification: contactsLeftEye,
brand: "MyBrand Name",
dateIssued: dateIssued,
expirationDate: expirationDate,
device: HKDevice.local(),
metadata: nil)Then 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) ***")
}Finally, add an image or PDF of the prescription to the sample as an attachment.
// 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) ***")
}