From 2f4394ba474bff9fafdc5cfa63a5f37932ec8df2 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Tue, 28 Jun 2016 18:06:09 -0400 Subject: [PATCH] added body_saveHeight method --- .../RCTAppleHealthKit+Methods_Body.h | 3 +++ .../RCTAppleHealthKit+Methods_Body.m | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h index 780f372..0dae9bb 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 30f64fd..383dd5e 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]; -- 2.26.2