diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index 0dae9bba7be43bb91fde4775cc0abe527bf1d3b2..0503822855a4b007016ee2207483e09d2d812573 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -14,6 +14,7 @@ - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_saveBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 59596cef191db473e50b9d7a937a8196dc09611f..e8520d398af8af006b95db77efbd826247227d5f 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -40,19 +40,15 @@ - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { double weight= [[input objectForKey:@"weight"] doubleValue]; + NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; -// HKUnit *poundUnit = [HKUnit poundUnit]; HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input]; if(unit == nil){ unit = [HKUnit poundUnit]; } HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:unit doubleValue:weight]; - HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; -// NSDate *now = [NSDate date]; - NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; - HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:sampleDate endDate:sampleDate]; [self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) { @@ -93,6 +89,29 @@ } + +- (void)body_saveBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + double bmi = [RCTAppleHealthKit doubleValueFromOptions:input]; + NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; + HKUnit *unit = [HKUnit countUnit]; + + HKQuantity *bmiQuantity = [HKQuantity quantityWithUnit:unit doubleValue:bmi]; + HKQuantityType *bmiType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex]; + HKQuantitySample *bmiSample = [HKQuantitySample quantitySampleWithType:bmiType quantity:bmiQuantity startDate:sampleDate endDate:sampleDate]; + + [self.healthStore saveObject:bmiSample withCompletion:^(BOOL success, NSError *error) { + if (!success) { + NSLog(@"An error occured saving the bmi sample %@. In your app, try to handle this gracefully. The error was: %@.", bmiSample, error); + callback(@[RCTMakeError(@"An error occured saving the bmi sample", nil, nil)]); + return; + } + callback(@[[NSNull null], @(bmi)]); + }]; +} + + + - (void)body_getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 628b16abbbbabc347c6f0e4eeab7694ee3a72d71..db7b2fd0440ad9bcefea60b406d011e265ed0f08 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -56,6 +56,11 @@ RCT_EXPORT_METHOD(getLatestBmi:(NSDictionary *)input callback:(RCTResponseSender [self body_getLatestBodyMassIndex:input callback:callback]; } +RCT_EXPORT_METHOD(saveBmi:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self body_saveBodyMassIndex:input callback:callback]; +} + RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) {