如何在iOS中阅读HealthKit的血压数据?

时间:2016-01-26 03:26:04

标签: objective-c health-kit

我在试图弄清楚如何阅读HealthKit中的血压数据时遇到了问题,特别是因为了解了目标C的HKCorrelationQuery如何对血压起作用,在开发者网站或教程中没有详细记录。

2 个答案:

答案 0 :(得分:4)

我使用以下代码通过HealthKit读取血压数据。我发现你不能直接读取收缩压或舒张压值。您需要为血压数据制作HKCorrelationQuery,然后对于每个相关性,您需要进行一些挖掘以便最终获得血压值。希望这有帮助!

- (void)readBloodPressure {

HKQuantityType *systolicType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic];
HKQuantityType *diastolicType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureDiastolic];
HKCorrelationType *bloodPressureType =
[HKCorrelationType correlationTypeForIdentifier:HKCorrelationTypeIdentifierBloodPressure];

HKCorrelationQuery *query =
[[HKCorrelationQuery alloc]
 initWithType:bloodPressureType predicate:nil
 samplePredicates:nil
 completion:^(HKCorrelationQuery *query, NSArray *correlations, NSError *error) {
     if (correlations == nil) {
         // Provide proper error handling here...
         NSLog(@"An error occurred while searching for blood pressure data %@",
               error.localizedDescription);
         abort();
     }
     for (HKCorrelation *correlation in correlations) {
          HKQuantitySample *systolicSample = [[correlation objectsForType:systolicType] anyObject];
         HKQuantity *systolicQuantity = [systolicSample quantity];
         HKQuantitySample *diastolicSample = [[correlation objectsForType:diastolicType] anyObject];
         HKQuantity *diastolicQuantity = [diastolicSample quantity];
         double systolicd = [systolicQuantity doubleValueForUnit:[HKUnit millimeterOfMercuryUnit]];
         double diastolicd = [diastolicQuantity doubleValueForUnit:[HKUnit millimeterOfMercuryUnit]];
         NSLog(@"Systolic %f",systolicd);
         NSLog(@"Diastolic %f",diastolicd);
         NSLog(@"Date %@",systolicSample.startDate);

         [self saveBloodPressureIntoApp:systolicd withDiastolic:diastolicd withDate:systolicSample.startDate];


     }

 }];

[self.healthStore executeQuery:query];

}

答案 1 :(得分:2)

可以直接从HealthKit条目的血压读取收缩压和舒张压值:

NSSet *querySet = [NSSet setWithObjects:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic],
                    [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureDiastolic], nil];

for (HKQuantityType *quantityType in querySet) {
    HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType predicate:nil limit:HKObjectQueryNoLimit sortDescriptors:nil resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
    if (results && results.count > 0) {
      dispatch_async(dispatch_get_main_queue(), ^{
        // Do something with results, which will be an array of HKQuantitySample objects
      });
}

然后,你只需要做一些挖掘就可以从这些物体中获得你想要的任何信息,正如奥利弗所说。