使用objective-c如何计算健康试剂盒中的体重指数和体脂百分比



在我的应用程序中,我正在集成健康工具包框架。我的项目要求是,我想把体脂百分比和瘦体重值从我的应用程序推到健康工具包应用程序中。所以我是这样写的。

-(void)viewDidLoad
{
NSString *bodymassIndex=@"60";
double index=[bodymassIndex doubleValue];
[[GSHealthKitManager sharedManager]saveBodyMassIndexintoHealthstore:index];
// For Body Mass
NSString *bodymass=@"40";
double mass=[bodymass doubleValue];
[[GSHealthKitManager sharedManager]saveBodyMassintoHealthstore:mass];
}
- (void)saveBodyMassintoHealthstore:(double)width
{
HKUnit *massUnit = [HKUnit poundUnit];
HKQuantity * weightQuantity = [HKQuantity quantityWithUnit:massUnit doubleValue:width];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass];
NSDate *now = [NSDate date];
HKQuantitySample *weightsample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:now endDate:now];
[self.healthStore saveObject:weightsample withCompletion:^(BOOL  success, NSError *error){
if (!success){
NSLog(@"Error");
}
}];
}
- (void)saveBodyMassIndexintoHealthstore:(double)weight
{
HKUnit *massUnit = [HKUnit mileUnit];
HKQuantity * weightQuantity = [HKQuantity quantityWithUnit:massUnit doubleValue:weight];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex];
NSDate *now = [NSDate date];
HKQuantitySample *weightsample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:now endDate:now];
[self.healthStore saveObject:weightsample withCompletion:^(BOOL  success, NSError *error){
if (!success)
{
NSLog(@"Error");
}
}];
}

但我的价值观并没有体现在健康工具包应用程序中。所以请引导我任何人。我不明白问题到底出在哪里。提前谢谢。。

试试这个:

- (void)saveBodyMassIndexintoHealthstore:(double)weight {
    // Each quantity consists of a value and a unit.
    HKUnit *kilogramUnit = [HKUnit gramUnitWithMetricPrefix:HKMetricPrefixKilo];
    HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:kilogramUnit doubleValue:weight];
    HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
    NSDate *now = [NSDate date];
    // For every sample, we need a sample type, quantity and a date.
    HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:now endDate:now];
    [self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"Error while saving weight (%f) to Health Store: %@.", weight, error);
        }
    }];
}

最新更新