如何使用HKObserverquery阅读iOS Healthkit血压(收缩压,舒张压)?

时间:2015-03-16 09:26:33

标签: ios objective-c health-kit

通过使用以下代码,当healthkit中有数据时,我可以使用观察者查询自动读取心率数据。

HKSampleType * readGlucoseType;

        readGlucoseType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];


    query = [[HKObserverQuery alloc]initWithSampleType:readGlucoseType
                                             predicate:nil
                                         updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)
             {

                 if (!error)
                 {
                     [self handleHKQueryResponse:query completionHandler:completionHandler errorCode:error];
                 }
                 else
                 {
                     NSLog(@"Observerquery Error");

                     if (completionHandler)
                     {
                         completionHandler();
                     }
                 }
             }];

    [self.healthStore executeQuery:query];

我如何使用观察者查询从健康套件中读取血压数据?

3 个答案:

答案 0 :(得分:4)

更新了Xcode 9.2的答案:

func readSampleByBloodPressure()
    {
         guard let type = HKQuantityType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.bloodPressure),
        let systolicType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureSystolic),
        let diastolicType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureDiastolic) else {

            return
    }

    let sampleQuery = HKSampleQuery(sampleType: type, predicate: nil, limit: 0, sortDescriptors: nil) { (sampleQuery, results, error) in
        if let dataList = results as? [HKCorrelation] {
            for data in dataList
            {
                if let data1 = data.objects(for: systolicType).first as? HKQuantitySample,
                    let data2 = data.objects(for: diastolicType).first as? HKQuantitySample {

                    let value1 = data1.quantity.doubleValue(for: HKUnit.millimeterOfMercury())
                    let value2 = data2.quantity.doubleValue(for: HKUnit.millimeterOfMercury())

                    print("\(value1) / \(value2)")
                }
            }
        }
    }
    healthStore.execute(sampleQuery)
    }

答案 1 :(得分:1)

有可能你来到这里寻找解决方案如何从swift中读取healthkit的血压。话虽如此,这是我的快速解决方案。

func readSampleByBloodPressure()
{

    let past = NSDate.distantPast() as! NSDate
    let now   = NSDate()
    let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: true)
    let type = HKQuantityType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierBloodPressure)
    let sampleQuery = HKSampleQuery(sampleType: type, predicate: nil, limit: 0, sortDescriptors: [sortDescriptor])
        { (sampleQuery, results, error ) -> Void in

            let dataLst = results as? [HKCorrelation];

            for var index=0;index<dataLst!.count;++index
            {

                let data1 = (dataLst![index].objectsForType(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureSystolic))).first as? HKQuantitySample
                let data2 = dataLst![index].objectsForType(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureDiastolic)).first as? HKQuantitySample

                if let value1 = data1!.quantity?.doubleValueForUnit(HKUnit.millimeterOfMercuryUnit()) , value2 = data2!.quantity?.doubleValueForUnit(HKUnit.millimeterOfMercuryUnit()) {
                    println(value1)
                    println(value2)
                }
            }

    }
    self.healthKitStore.executeQuery(sampleQuery)       

}

答案 2 :(得分:-1)

以下是从健康套件中获取血压数据的示例代码。

    HKCorrelationType *correlationType = [HKCorrelationType correlationTypeForIdentifier:HKCorrelationTypeIdentifierBloodPressure];

[self.healthStore enableBackgroundDeliveryForType:sampleType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {}];

HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:sampleType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
        if (!error)
             {
                 [self handleHKQueryResponse:query completionHandler:completionHandler errorCode:error];
             }
             else
             {
                 NSLog(@"Observerquery Error");

                 if (completionHandler)
                 {
                     completionHandler();
                 }
             }
    [self.healthStore executeQuery:query];