我在试图弄清楚如何在 HealthKit 中读取血压数据时遇到了问题,特别是因为了解 HKCorrelationQuery 如何用于 Objective C 的血压并没有在开发人员网站或教程中得到很好的记录。
最佳答案
我使用以下代码通过 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];
}
关于objective-c - 如何在 iOS 中读取 HealthKit 的血压数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35006586/