Russ
Russ

Reputation: 576

HealthKit Always Returns No Results when reading Blood Pressure

I have been trying various options to get results back, but no luck. I took the code from another question and tried to make it work, but still nothing. I manually entered the data into health as well as took readings from a BP monitor, so I know there is data there. No errors reported.

    func readSampleByBloodPressure()
        {
            let past = Date.distantPast
            let now   = Date()
            let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: true)
            let type = HKQuantityType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.bloodPressure)
            let sampleQuery = HKSampleQuery(sampleType: type!, predicate: nil, limit: 0, sortDescriptors: [sortDescriptor])
            { (sampleQuery, results, error ) -> Void in

                let dataLst = results as? [HKCorrelation];

                for data in dataLst!
                {

                    let data1 = (data.objects(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureSystolic)!)).first as? HKQuantitySample
                    let data2 = data.objects(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureDiastolic)!).first as? HKQuantitySample
                print("Data Found")
                /*
                    if let value1 = data1!.quantity.doubleValue(for:     HKUnit.millimeterOfMercury()) , let value2 = data2!.quantity.doubleValue(for: HKUnit.millimeterOfMercury())
                    {
                        print(value1)
                        print(value2)
                    }
                     */
                }

            }
            self.healthStore?.execute(sampleQuery)
        }
}

Upvotes: 0

Views: 624

Answers (1)

RTB
RTB

Reputation: 192

This is caused by failing to authorize both systolic and diastolic blood pressure readings. In the authorization, when you get the popup from Health Kit, even though you select Authorize All, All implies only what is showing in the list presented, not everything HealthKit has available. In your auth code, you need to specify each thing you wish, which will then show up on the auth popup.

As a side note. If you have done this and if you check in the health app and see that what you want is authorized and it still isn't working, try toggling the setting. Apparently, the initial approval doesn't always take.

Upvotes: 1

Related Questions