Recording Blood Pressure Data in HealthKit

We're currently in the process of expanding the capabilities of one our Apple Watch apps, HealthFace. One of the challenges we faced was figuring out how to record blood pressure data into HealthKit.

Typically, recording data into HealthKit is quite straightforward: You create a new HKQuantitySample and save it to your HKHealthStore object.

Recording a blood pressure reading in HealthKit is slightly more complex though, since it is a correlation type made up of two values: the systolic pressure and the diastolic pressure.

For example, a commonly-used blood pressure reading is 120/80 ("120 over 80"), measured in millimetres of mercury (mmHg). The value of 120 is the systolic blood pressure (maximum pressure during one heart beat), while 80 is the diastolic blood pressure (minimum pressure between two heart beats).

Both values need need to be recorded together, since they make up a single reading. You can achieve this by saving a HKCorrelation object instead (made up of two HKQuantitySample objects.

Firstly, set up some useful data that is needed:

// Type of sample
let unit  = HKUnit.millimeterOfMercury()

// Date of sample
let start = Date()
let end   = start

Next, create the systolic sample:

let sQuantity = HKQuantity(unit: unit, doubleValue: 120)
let sType     = HKQuantityType.quantityType(forIdentifier: .bloodPressureSystolic)!
let sSample   = HKQuantitySample(
    type: sType,
    quantity: sQuantity,
    start: start, 
    end: end

Likewise, create the diastolic sample:

let dQuantity = HKQuantity(unit: unit, doubleValue: 80)
let dType     = HKQuantityType.quantityType(forIdentifier: .bloodPressureDiastolic)!
let dSample   = HKQuantitySample(
    type: dType,
    quantity: sQuantity,
    start: start,
    end: end

Now bind these together so they can be saved as a correlation:

let objects: Set<HKSample> = [ sSample, dSample ]

Next, create a new correlation object using the blood pressure correlation type:

let bpType = HKCorrelationType.correlationType(forIdentifier: .bloodPressure)!

let bloodPressure = HKCorrelation(
    type: bpType,
    start: start,
    end: end,
    objects: objects

And finally, save to HealthKit. This assumes you've already requested permission to write the data:

let healthStore = HKHealthStore() { success, error in
    // Called after save completed

And that's all there is to it!

Note: When requesting permission, you need to request permission on the .bloodPressureSystolic and .bloodPressureDiastolic types. It is not possible to request read or write permission on a correlation type.