0

Problem

We're integrating Healthkit in our existing iOS app. We've managed to get all the major data from Healthkit i.e. Running, Weight, etc but where we got stuck is when it comes to fetch meta data like- when user recorded that activity, was it automatically recorded or manually, what is the source of data, etc.

Please see the image below. This is the data we want to get.

We've used Objective-C.

Code

- (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]);

            });
        }
    }];
}

Image

enter image description here

4

1 回答 1

0

这是获取 wasUserEntered 键值的代码

BOOL wasUserEntered = [[mostRecentQuantity.metadata objectForKey:HKMetadataKeyWasUserEntered] boolValue];
于 2015-03-19T13:51:53.733 回答