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

时间:2016-04-05 09:47:46

标签: ios objective-c health-kit

在我的应用程序中,我正在整合健康工具包框架。我的项目要求是我想将身体脂肪百分比和瘦体重值从我的应用程序推送到Health kit应用程序。所以我这样写。

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

}

}];

}

但我的价值观没有在健康套件申请中显示。所以请指导我任何人。我无法理解问题究竟在哪里。提前谢谢..

1 个答案:

答案 0 :(得分:0)

试试这个:

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