From 76d9f7ef4a13e79cce814da1a0b74815f71dadf2 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Sun, 26 Jun 2016 22:37:11 -0400 Subject: [PATCH] created new categories for body and fitness identifiers and refactored code to reflect changes --- RCTAppleHealthKit.xcodeproj/project.pbxproj | 12 ++ .../RCTAppleHealthKit+Methods_Body.h | 16 ++ .../RCTAppleHealthKit+Methods_Body.m | 60 +++++++ .../RCTAppleHealthKit+Methods_Fitness.h | 15 ++ .../RCTAppleHealthKit+Methods_Fitness.m | 30 ++++ RCTAppleHealthKit/RCTAppleHealthKit+Queries.m | 43 +++-- .../RCTAppleHealthKit+TypesAndPermissions.m | 2 - RCTAppleHealthKit/RCTAppleHealthKit.h | 6 +- RCTAppleHealthKit/RCTAppleHealthKit.m | 147 +++++++++--------- 9 files changed, 231 insertions(+), 100 deletions(-) create mode 100644 RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h create mode 100644 RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m create mode 100644 RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h create mode 100644 RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m diff --git a/RCTAppleHealthKit.xcodeproj/project.pbxproj b/RCTAppleHealthKit.xcodeproj/project.pbxproj index 6ef3d6e..073f1be 100644 --- a/RCTAppleHealthKit.xcodeproj/project.pbxproj +++ b/RCTAppleHealthKit.xcodeproj/project.pbxproj @@ -12,6 +12,8 @@ 3774C89B1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C89A1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m */; }; 3774C89E1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */; }; 3774C8A11D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */; }; + 3774C8D41D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C8D31D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m */; }; + 3774C8D71D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C8D61D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -37,6 +39,10 @@ 3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+TypesAndPermissions.m"; sourceTree = ""; }; 3774C89F1D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Utils.h"; sourceTree = ""; }; 3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Utils.m"; sourceTree = ""; }; + 3774C8D21D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Body.h"; sourceTree = ""; }; + 3774C8D31D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Body.m"; sourceTree = ""; }; + 3774C8D51D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Fitness.h"; sourceTree = ""; }; + 3774C8D61D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Fitness.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -77,6 +83,10 @@ 3774C89D1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m */, 3774C89F1D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.h */, 3774C8A01D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m */, + 3774C8D21D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.h */, + 3774C8D31D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m */, + 3774C8D51D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.h */, + 3774C8D61D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m */, ); path = RCTAppleHealthKit; sourceTree = ""; @@ -139,8 +149,10 @@ files = ( 3774C89B1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m in Sources */, 3774C8A11D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m in Sources */, + 3774C8D41D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m in Sources */, 3774C8931D2092F20000B3F3 /* RCTAppleHealthKit.m in Sources */, 3774C89E1D2095850000B3F3 /* RCTAppleHealthKit+TypesAndPermissions.m in Sources */, + 3774C8D71D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h new file mode 100644 index 0000000..fcff8ef --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h @@ -0,0 +1,16 @@ +// +// RCTAppleHealthKit+Methods_Body.h +// RCTAppleHealthKit +// +// Created by Greg Wilson on 2016-06-26. +// Copyright © 2016 Greg Wilson. All rights reserved. +// + +#import "RCTAppleHealthKit.h" + +@interface RCTAppleHealthKit (Methods_Body) + +- (void)body_getCurrentWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m new file mode 100644 index 0000000..1ec97c1 --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -0,0 +1,60 @@ +// +// RCTAppleHealthKit+Methods_Body.m +// RCTAppleHealthKit +// +// Created by Greg Wilson on 2016-06-26. +// Copyright © 2016 Greg Wilson. All rights reserved. +// + +#import "RCTAppleHealthKit+Methods_Body.h" +#import "RCTAppleHealthKit+Queries.h" + +@implementation RCTAppleHealthKit (Methods_Body) + +- (void)body_getCurrentWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + // Query to get the user's latest weight, if it exists. + HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; + + [self fetchMostRecentQuantitySampleOfType:weightType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error) { + if (!mostRecentQuantity) { + NSLog(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully."); + callback(@[RCTMakeError(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); + } + else { + // Determine the weight in the required unit. + HKUnit *weightUnit = [HKUnit poundUnit]; + double usersWeight = [mostRecentQuantity doubleValueForUnit:weightUnit]; + + callback(@[[NSNull null], @(usersWeight)]); + } + }]; +} + + + + +- (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + double weight= [[input objectForKey:@"weight"] doubleValue]; + + HKUnit *poundUnit = [HKUnit poundUnit]; + HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:poundUnit doubleValue:weight]; + + HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; + 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(@"An error occured saving the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error); + callback(@[RCTMakeError(@"An error occured saving the weight sample", nil, nil)]); + return; + // abort(); + } + callback(@[[NSNull null], @(weight)]); + }]; +} + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h new file mode 100644 index 0000000..150f274 --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -0,0 +1,15 @@ +// +// RCTAppleHealthKit+Methods_Fitness.h +// RCTAppleHealthKit +// +// Created by Greg Wilson on 2016-06-26. +// Copyright © 2016 Greg Wilson. All rights reserved. +// + +#import "RCTAppleHealthKit.h" + +@interface RCTAppleHealthKit (Methods_Fitness) + +- (void)fitness_getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m new file mode 100644 index 0000000..31ac748 --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -0,0 +1,30 @@ +// +// RCTAppleHealthKit+Methods_Fitness.m +// RCTAppleHealthKit +// +// Created by Greg Wilson on 2016-06-26. +// Copyright © 2016 Greg Wilson. All rights reserved. +// + +#import "RCTAppleHealthKit+Methods_Fitness.h" +#import "RCTAppleHealthKit+Queries.h" + +@implementation RCTAppleHealthKit (Methods_Fitness) + +- (void)fitness_getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; + HKUnit *stepsUnit = [HKUnit countUnit]; + + [self fetchSumOfSamplesTodayForType:stepCountType unit:stepsUnit completion:^(double totalSteps, NSError *error) { + if (!totalSteps) { + NSLog(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully."); + callback(@[RCTMakeError(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); + return; + } + + callback(@[[NSNull null], @(totalSteps)]); + }]; +} + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 2dba35b..172615e 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -13,29 +13,26 @@ - (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *, NSError *))completion { NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO]; - - // Since we are interested in retrieving the user's latest sample, we sort the samples in descending order, and set the limit to 1. We are not filtering the data, and so the predicate is set to nil. + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType - predicate:predicate - limit:1 - sortDescriptors:@[timeSortDescriptor] - resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { - if (!results) { - if (completion) { - completion(nil, error); - } - - return; - } - + predicate:predicate + limit:1 + sortDescriptors:@[timeSortDescriptor] + resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { + if (!results) { if (completion) { - // If quantity isn't in the database, return nil in the completion block. - HKQuantitySample *quantitySample = results.firstObject; - HKQuantity *quantity = quantitySample.quantity; - - completion(quantity, error); + completion(nil, error); } - }]; + return; + } + + if (completion) { + // If quantity isn't in the database, return nil in the completion block. + HKQuantitySample *quantitySample = results.firstObject; + HKQuantity *quantity = quantitySample.quantity; + completion(quantity, error); + } + }]; [self.healthStore executeQuery:query]; } @@ -45,12 +42,14 @@ - (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler { NSPredicate *predicate = [RCTAppleHealthKit predicateForSamplesToday]; - HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType quantitySamplePredicate:predicate options:HKStatisticsOptionCumulativeSum completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) { + HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:quantityType + quantitySamplePredicate:predicate + options:HKStatisticsOptionCumulativeSum + completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) { HKQuantity *sum = [result sumQuantity]; if (completionHandler) { double value = [sum doubleValueForUnit:unit]; - completionHandler(value, error); } }]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index c940a0f..f529613 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -80,7 +80,6 @@ } return readPermSet; - // return [self dataTypesToRead]; } @@ -99,7 +98,6 @@ } return writePermSet; - // return [self dataTypesToWrite]; } @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.h b/RCTAppleHealthKit/RCTAppleHealthKit.h index 66b9b3a..f18442d 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit.h @@ -18,9 +18,9 @@ - (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback; - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; -- (void)getHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; -- (void)saveHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; -- (void)getHealthKitUserStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +//- (void)getHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +//- (void)saveHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +//- (void)getHealthKitUserStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index e2efcd9..5248fca 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -11,6 +11,9 @@ #import "RCTAppleHealthKit+TypesAndPermissions.h" #import "RCTAppleHealthKit+Utils.h" +#import "RCTAppleHealthKit+Methods_Body.h" +#import "RCTAppleHealthKit+Methods_Fitness.h" + @implementation RCTAppleHealthKit RCT_EXPORT_MODULE(); @@ -28,17 +31,17 @@ RCT_EXPORT_METHOD(initHealthKit:(NSDictionary *)input callback:(RCTResponseSende RCT_EXPORT_METHOD(getCurrentWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { - [self getHealthKitUserWeight:input callback:callback]; + [self body_getCurrentWeight:input callback:callback]; } RCT_EXPORT_METHOD(saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { - [self saveHealthKitUserWeight:input callback:callback]; + [self body_saveWeight:input callback:callback]; } RCT_EXPORT_METHOD(getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { - [self getHealthKitUserStepCount:input callback:callback]; + [self fitness_getStepCountForToday:input callback:callback]; } RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) @@ -66,7 +69,7 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock NSSet *writeDataTypes = [self dataTypesToWrite]; NSSet *readDataTypes = [self dataTypesToRead]; - // get permissions from input object provided by JS options object argument + // get permissions from input object provided by JS options argument NSDictionary* permissions =[input objectForKey:@"permissions"]; if(permissions != nil){ NSArray* readPermsArray = [permissions objectForKey:@"read"]; @@ -94,82 +97,80 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock callback(@[[NSNull null], @true]); }); } - - }]; } else { callback(@[RCTMakeError(@"HealthKit not available....", nil, nil)]); } } +// +//- (void)getHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +//{ +//// NSMassFormatter *massFormatter = [[NSMassFormatter alloc] init]; +//// massFormatter.unitStyle = NSFormattingUnitStyleLong; +// +// // NSMassFormatterUnit weightFormatterUnit = NSMassFormatterUnitPound; +// // NSString *weightUnitString = [massFormatter unitStringFromValue:10 unit:weightFormatterUnit]; +// // NSString *localizedWeightUnitDescriptionFormat = NSLocalizedString(@"Weight (%@)", nil); +// +// // Query to get the user's latest weight, if it exists. +// HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; +// +// [self fetchMostRecentQuantitySampleOfType:weightType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error) { +// if (!mostRecentQuantity) { +// NSLog(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully."); +// callback(@[RCTMakeError(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); +// } +// else { +// // Determine the weight in the required unit. +// HKUnit *weightUnit = [HKUnit poundUnit]; +// double usersWeight = [mostRecentQuantity doubleValueForUnit:weightUnit]; +// +// callback(@[[NSNull null], @(usersWeight)]); +// } +// }]; +//} -- (void)getHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback -{ -// NSMassFormatter *massFormatter = [[NSMassFormatter alloc] init]; -// massFormatter.unitStyle = NSFormattingUnitStyleLong; - - // NSMassFormatterUnit weightFormatterUnit = NSMassFormatterUnitPound; - // NSString *weightUnitString = [massFormatter unitStringFromValue:10 unit:weightFormatterUnit]; - // NSString *localizedWeightUnitDescriptionFormat = NSLocalizedString(@"Weight (%@)", nil); - - // Query to get the user's latest weight, if it exists. - HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; - - [self fetchMostRecentQuantitySampleOfType:weightType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error) { - if (!mostRecentQuantity) { - NSLog(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully."); - callback(@[RCTMakeError(@"Either an error occured fetching the user's weight information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); - } - else { - // Determine the weight in the required unit. - HKUnit *weightUnit = [HKUnit poundUnit]; - double usersWeight = [mostRecentQuantity doubleValueForUnit:weightUnit]; - - callback(@[[NSNull null], @(usersWeight)]); - } - }]; -} - - -- (void)saveHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback -{ - double weight= [[input objectForKey:@"weight"] doubleValue]; - - HKUnit *poundUnit = [HKUnit poundUnit]; - HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:poundUnit doubleValue:weight]; - - HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; - 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(@"An error occured saving the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error); - callback(@[RCTMakeError(@"An error occured saving the weight sample", nil, nil)]); - return; - // abort(); - } - callback(@[[NSNull null], @(weight)]); - }]; -} - - -- (void)getHealthKitUserStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback -{ - HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; - HKUnit *stepsUnit = [HKUnit countUnit]; - - [self fetchSumOfSamplesTodayForType:stepCountType unit:stepsUnit completion:^(double totalSteps, NSError *error) { - if (!totalSteps) { - NSLog(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully."); - callback(@[RCTMakeError(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); - return; - } - - callback(@[[NSNull null], @(totalSteps)]); - }]; -} +// +//- (void)saveHealthKitUserWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +//{ +// double weight= [[input objectForKey:@"weight"] doubleValue]; +// +// HKUnit *poundUnit = [HKUnit poundUnit]; +// HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:poundUnit doubleValue:weight]; +// +// HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; +// 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(@"An error occured saving the weight sample %@. In your app, try to handle this gracefully. The error was: %@.", weightSample, error); +// callback(@[RCTMakeError(@"An error occured saving the weight sample", nil, nil)]); +// return; +// // abort(); +// } +// callback(@[[NSNull null], @(weight)]); +// }]; +//} + + +//- (void)getHealthKitUserStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +//{ +// HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; +// HKUnit *stepsUnit = [HKUnit countUnit]; +// +// [self fetchSumOfSamplesTodayForType:stepCountType unit:stepsUnit completion:^(double totalSteps, NSError *error) { +// if (!totalSteps) { +// NSLog(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully."); +// callback(@[RCTMakeError(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); +// return; +// } +// +// callback(@[[NSNull null], @(totalSteps)]); +// }]; +//} -- 2.26.2