diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index 780f37221dccb03ad3efb3ad58a106279133a7b1..0dae9bba7be43bb91fde4775cc0abe527bf1d3b2 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -16,6 +16,9 @@ - (void)body_getLatestBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getMostRecentHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + + - (void)body_getMostRecentBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getMostRecentLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 30f64fd9b9887f4bc88975260d40538d19d8ea66..383dd5e41edbc27956940ca8ed95f8a5406a0204 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -104,6 +104,33 @@ } +- (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ +// NSNumber *valueFromOptions = [RCTAppleHealthKit numericValueFromOptions:input]; +// double height = [[input objectForKey:@"value"] doubleValue]; + double height = [RCTAppleHealthKit doubleValueFromOptions:input]; + NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; + + HKUnit *heightUnit = [HKUnit inchUnit]; + HKQuantity *heightQuantity = [HKQuantity quantityWithUnit:heightUnit doubleValue:height]; + + HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; +// NSDate *now = [NSDate date]; + +// HKQuantitySample *heightSample = [HKQuantitySample quantitySampleWithType:heightType quantity:heightQuantity startDate:now endDate:now]; + HKQuantitySample *heightSample = [HKQuantitySample quantitySampleWithType:heightType quantity:heightQuantity startDate:sampleDate endDate:sampleDate]; + + [self.healthStore saveObject:heightSample withCompletion:^(BOOL success, NSError *error) { + if (!success) { + NSLog(@"An error occured saving the height sample %@. In your app, try to handle this gracefully. The error was: %@.", heightSample, error); + callback(@[RCTMakeError(@"An error occured saving the height sample", nil, nil)]); + return; + } + callback(@[[NSNull null], @(height)]); + }]; +} + + - (void)body_getMostRecentBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKQuantityType *bodyFatPercentType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage];