diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h index 64041e765989b5508cc464f180f2df314b7ab12d..73438fe45b41c11bc4fdd3e28277b9f7d3b0d96c 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.h @@ -10,12 +10,6 @@ @interface RCTAppleHealthKit (TypesAndPermissions) -- (NSSet *)dataTypesToWrite; -- (NSSet *)dataTypesToRead; - -- (NSDictionary *)readPermsDict; -- (NSDictionary *)writePermsDict; - - (NSSet *)getReadPermsFromOptions:(NSArray *)options; - (NSSet *)getWritePermsFromOptions:(NSArray *)options; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index c0729f7372dad0b2fe96a451a497ca80ab22da3a..c9e8aed55b7423ae4dfc2843f07ca9d2221cef3e 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -12,35 +12,6 @@ #pragma mark - HealthKit Permissions - -// Returns the types of data that Fit wishes to write to HealthKit. -- (NSSet *)dataTypesToWrite { - HKQuantityType *dietaryCalorieEnergyType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed]; - HKQuantityType *activeEnergyBurnType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned]; - HKQuantityType *heightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; - HKQuantityType *weightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; - - return [NSSet setWithObjects:dietaryCalorieEnergyType, activeEnergyBurnType, heightType, weightType, nil]; -} - - - -// Returns the types of data that Fit wishes to read from HealthKit. -- (NSSet *)dataTypesToRead { - HKQuantityType *dietaryCalorieEnergyType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed]; - HKQuantityType *activeEnergyBurnType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned]; - HKQuantityType *heightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; - HKQuantityType *weightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; - - HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; - - HKCharacteristicType *birthdayType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth]; - HKCharacteristicType *biologicalSexType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex]; - - return [NSSet setWithObjects:dietaryCalorieEnergyType, activeEnergyBurnType, heightType, weightType, stepCountType, birthdayType, biologicalSexType, nil]; -} - - - (NSDictionary *)readPermsDict { NSDictionary *readPerms = @{ @"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed], diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 31f8dd226d80541650e32af545b10f62c43dd611..0638690b2ddac71bbae60a342db3a715744df978 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -70,10 +70,9 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock self.healthStore = [[HKHealthStore alloc] init]; if ([HKHealthStore isHealthDataAvailable]) { - - NSSet *writeDataTypes = [self dataTypesToWrite]; - NSSet *readDataTypes = [self dataTypesToRead]; - + NSSet *writeDataTypes; + NSSet *readDataTypes; + // get permissions from input object provided by JS options argument NSDictionary* permissions =[input objectForKey:@"permissions"]; if(permissions != nil){ @@ -89,13 +88,22 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock if(writePerms != nil) { writeDataTypes = writePerms; } + } else { + callback(@[RCTMakeError(@"permissions must be provided in the initialization options", nil, nil)]); + return; + } + + // make sure at least 1 read or write permission is provided + if(!writeDataTypes && !readDataTypes){ + callback(@[RCTMakeError(@"at least 1 write or read permission must be set in options.permissions", nil, nil)]); + return; } - - + [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) { if (!success) { - NSLog(@"You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %@. If you're using a simulator, try it on a device.", error); - callback(@[RCTMakeError(@"You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %@. If you're using a simulator, try it on a device.", nil, nil)]); + NSString *errMsg = [NSString stringWithFormat:@"Error with HealthKit authorization: %@", error]; + NSLog(errMsg); + callback(@[RCTMakeError(errMsg, nil, nil)]); return; } else { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ @@ -111,7 +119,13 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock - (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { - callback(@[[NSNull null], @"ReactNative Apple HealthKit Native Module. Created By Greg Wilson."]); + NSDictionary *info = @{ + @"name" : @"react-native-apple-healthkit", + @"description" : @"A React Native bridge module for interacting with Apple HealthKit data", + @"className" : @"RCTAppleHealthKit", + @"author": @"Greg Wilson", + }; + callback(@[[NSNull null], info]); }