Search code examples
iosswifthealthkit

HealthKit Blood Oxygen SPO2


With the series 6 Apple Watch, you can now get a measure of your SP02, hemoglobin content in your blood oxygen. The health app on the iPhone shows you all the measurements in the Respiratory section. This is a critical component for COVID patients.

I have not been able to find anyway to access this information programatically.

I have checked all HKObjectTypes in the latest Apple documentation. Is this information currently available to iOS developers?

Any information would be of great use as several researchers are requesting it.


Solution

  • Ok, I am being told that this is the same as Oxygen Saturation.Here is the code I use to query HK for Oxygen Saturation:

        // Get SPO2
    func getOxygenSaturation()
    {
        // Type is SPO2 SDNN
        let osType:HKQuantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.oxygenSaturation)!
    
        let predicate = HKQuery.predicateForSamples(withStart: Date.distantPast, end: Date(), options: .strictEndDate)
        let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
        let osUnit:HKUnit = HKUnit(from: "%")
    
        
        let osQuery = HKSampleQuery(sampleType: osType,
                                     predicate: predicate,
                                     limit: 10,
                                     sortDescriptors: [sortDescriptor]) { (query, results, error) in
            guard error == nil else { print("error"); return }
            
            // Get the array of results from the sample query
            let sampleArray:[HKSample]? = results!
            
            // Loop through the array of rsults
            for (_, sample) in sampleArray!.enumerated()
            {
                // Be sure something is there
                if let currData:HKQuantitySample = sample as? HKQuantitySample
                {
                    let os: Double = (currData.quantity.doubleValue(for: osUnit) * 100.0)
                    let d1: Date = currData.startDate
                    let str1 = SwiftLib.returnDateAndTimeWithTZ(date: d1, info: self.info!)
    
                    Dispatch.DispatchQueue.main.async {
                        
                        self.tvOxygenValue.text = String(format: "%.0f%@", os, "%");
                        self.tvOxygenDate.text = str1
                        //print("\(os)");
                    }
                }
            }
            
            print("Done")
            self.loadAndDisplayActivityInformation()
        }
    
        healthStore!.execute(osQuery)
    }