如何使用HealthKit从iHealth获取元数据?

时间:2014-12-10 14:12:48

标签: objective-c ios8 health-kit

问题

我们正在将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]);

            });
        }
    }];
}

图片

enter image description here

1 个答案:

答案 0 :(得分:0)

以下是获取wasUserEntered键值

的代码
BOOL wasUserEntered = [[mostRecentQuantity.metadata objectForKey:HKMetadataKeyWasUserEntered] boolValue];