diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index 086797426969dc6bd3c069fdbb3cccfe2eefffe8..d932312b99780f7088cf587952ca15c40a390db1 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -23,6 +23,8 @@ - (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + - (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 34fd328837a07105f0872461669cc942ab2c3531..cdf0563cff0a15b39fd13076ec1f6453206053ae 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -257,6 +257,30 @@ } +- (void)body_saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + + double percentage = [RCTAppleHealthKit doubleValueFromOptions:input]; + NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; + HKUnit *unit = [HKUnit percentUnit]; + + percentage = percentage / 100; + + HKQuantity *bodyFatPercentQuantity = [HKQuantity quantityWithUnit:unit doubleValue:percentage]; + HKQuantityType *bodyFatPercentType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage]; + HKQuantitySample *bodyFatPercentSample = [HKQuantitySample quantitySampleWithType:bodyFatPercentType quantity:bodyFatPercentQuantity startDate:sampleDate endDate:sampleDate]; + + [self.healthStore saveObject:bodyFatPercentSample withCompletion:^(BOOL success, NSError *error) { + if (!success) { + NSLog(@"error saving body fat percent sample: %@", error); + callback(@[RCTMakeError(@"error saving body fat percent sample", error, nil)]); + return; + } + callback(@[[NSNull null], @(percentage)]); + }]; +} + + - (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKQuantityType *leanBodyMassType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index c91c471347d69c74e01f5bf057293be6efa7880c..1d8486cf0cd657989ba81467246d276df7d3dbbd 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -99,6 +99,11 @@ RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCT [self body_getLatestBodyFatPercentage:input callback:callback]; } +RCT_EXPORT_METHOD(saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self body_saveBodyFatPercentage:input callback:callback]; +} + RCT_EXPORT_METHOD(getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self body_getLatestLeanBodyMass:input callback:callback]; diff --git a/README.md b/README.md index b5e2e20f773668c73718bddbb1f054a18e11e2bd..3232abe968ad0a79de5f660186e415c2a7774376 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,7 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) => * [saveMindfulSession](/docs/saveMindfulSession().md) * [saveWeight](/docs/saveWeight().md) * [saveSteps](/docs/saveSteps().md) + * [saveBodyFatPercentage](/docs/saveBodyFatPercentage().md) * [References](#references) ## Supported Apple Permissions @@ -171,6 +172,7 @@ The available Healthkit permissions to use with `initHealthKit` | StepCount | [HKQuantityTypeIdentifierStepCount](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierstepcount?language=objc) | ✓ | ✓ | | Steps | [HKQuantityTypeIdentifierSteps](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifiersteps?language=objc) | ✓ | ✓ | | Weight | [HKQuantityTypeIdentifierBodyMass](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodymass?language=objc) | ✓ | ✓ | +| BodyFatPercentage | [HKQuantityTypeIdentifierBodyFatPercentage](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodyfatpercentage?language=objc) | ✓ | ✓ | These permissions are exported as constants of the `rn-apple-healthkit` module. diff --git a/docs/saveBodyFatPercentage().md b/docs/saveBodyFatPercentage().md new file mode 100644 index 0000000000000000000000000000000000000000..e5f2bc39e36855e1c3a5e6673842f6cc2e5cbb30 --- /dev/null +++ b/docs/saveBodyFatPercentage().md @@ -0,0 +1,17 @@ +save a percentage body fat value to Healthkit + +`saveBodyFatPercentage` accepts an options object containing a percent value: +```javascript +let options = { + value: 16.7 // 16.7% +} +``` + +```javascript +AppleHealthKit.saveBodyFatPercentage(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + // body fat percentage successfully saved +}); +```