2016-01-26 71 views

回答

3

我用下面的代碼通過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]; 

} 
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 
     }); 
} 

然後,你就必須做一些挖掘得到你想要的任何信息,從這些對象,正如奧利弗所說。