-1

In my application I am integrating health kit framework. My project requirement is I want to push body fat percentage and Lean body mass values from my application to Health kit application. So I am writing like this.

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

}

}];

}

But my values are not showing in health kit application. So please guide me anybody. I can’t understand where is the issue exactly. Thanks in advance..

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
satya
  • 241
  • 3
  • 11
  • Have you used the debugger? – trojanfoe Apr 05 '16 at 09:49
  • @trojanfoe thanks for your response.. HKQuantitySample *weightsample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:now endDate:now]; This line of code i got following exception Terminating app due to uncaught exception '_HKObjectValidationFailureException', reason: 'HKQuantitySample 60 mi 2016-04-05 15:20:16 +0530 2016-04-05 15:20:16 +0530 requires unit of type (null). Incompatible unit: mi' – satya Apr 05 '16 at 09:52
  • @trojanfoe bodymassindex method i got above exception – satya Apr 05 '16 at 09:53
  • 1
    Add the complete crash log to your question, not to comments. – trojanfoe Apr 05 '16 at 09:53
  • @trojanfoe 'HKQuantitySample 60 mi 2016-04-05 15:24:51 +0530 2016-04-05 15:24:51 +0530 requires unit of type (null). Incompatible unit: mi' *** First throw call stack: ( 0 CoreFoundation 0x00bcba14 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x0068ce02 objc_exception_throw + 50 2 CoreFoundation 0x00bcb93d +[NSException raise:format:] + 141 – satya Apr 05 '16 at 09:57
  • 1
    ADD THE INFORMATION TO YOUR QUESTION! – trojanfoe Apr 05 '16 at 09:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108262/discussion-between-satya-and-trojanfoe). – satya Apr 05 '16 at 10:00
  • @trojanfoe plz come to chat room.. i m waiting for you.. i want to discuss with you regarding the issue – satya Apr 05 '16 at 10:02
  • I am not interested in the issue; only in you asking your question correctly. – trojanfoe Apr 05 '16 at 10:04
  • @Tejas Ardeshna will you plz help me if you have any idea – satya Apr 05 '16 at 10:49
  • @satya Go through this link will help you. http://code.tutsplus.com/tutorials/getting-started-with-healthkit-part-1--cms-24477 – Tejas Ardeshna Apr 05 '16 at 11:22

1 Answers1

0

Try this one:

- (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);
        }
    }];
}
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39