【发布时间】:2014-12-10 14:12:47
【问题描述】:
问题
我们正在将 Healthkit 集成到我们现有的 iOS 应用中。我们已经设法从 Healthkit 中获取所有主要数据,例如跑步、体重等,但我们遇到的困难是在获取 元数据 时,比如用户记录该活动时,它是否会自动记录或者手动,数据的来源是什么等。
请看下图。这就是我们要获取的数据。
我们使用了 Objective-C。
代码
- (void)updateUsers_BodyMassIndex
{
// for getting body mass index.
HKQuantityType *Distance_BodyMassIndex = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex];
NSLog(@"%@",Distance_BodyMassIndex);
// call query method for getting BodyMassIndex data
[self.healthStore aapl_mostRecentQuantitySampleOfType:Distance_BodyMassIndex predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error)
{
if (!mostRecentQuantity)
{
NSLog(@"Either an error occured fetching the user's height information or none has been stored yet. In your app, try to handle this gracefully.");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"not available BodyMassIndex");
});
}
else
{
// to get was user entered or not
HKQuantityType *BodyMassIndexuserentered = [HKQuantityType quantityTypeForIdentifier:HKMetadataKeyWasUserEntered];
// getting unit for body mass index.
HKUnit *DISTANCE_BodyMassIndexUnit = [HKUnit countUnit];
double BodyMassIndex = [mostRecentQuantity doubleValueForUnit:DISTANCE_BodyMassIndexUnit];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"available body mass index %@",[NSNumberFormatter localizedStringFromNumber:@(BodyMassIndex) numberStyle:NSNumberFormatterNoStyle]);
});
}
}];
}
图片
【问题讨论】:
标签: objective-c ios8 healthkit