Commit 88f1fe96 authored by Greg Wilson's avatar Greg Wilson

cleaned up error callback and removing refactored dead code

parent 7da1087a
...@@ -10,12 +10,6 @@ ...@@ -10,12 +10,6 @@
@interface RCTAppleHealthKit (TypesAndPermissions) @interface RCTAppleHealthKit (TypesAndPermissions)
- (NSSet *)dataTypesToWrite;
- (NSSet *)dataTypesToRead;
- (NSDictionary *)readPermsDict;
- (NSDictionary *)writePermsDict;
- (NSSet *)getReadPermsFromOptions:(NSArray *)options; - (NSSet *)getReadPermsFromOptions:(NSArray *)options;
- (NSSet *)getWritePermsFromOptions:(NSArray *)options; - (NSSet *)getWritePermsFromOptions:(NSArray *)options;
......
...@@ -12,35 +12,6 @@ ...@@ -12,35 +12,6 @@
#pragma mark - HealthKit Permissions #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 *)readPermsDict {
NSDictionary *readPerms = @{ NSDictionary *readPerms = @{
@"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed], @"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed],
......
...@@ -70,9 +70,8 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock ...@@ -70,9 +70,8 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock
self.healthStore = [[HKHealthStore alloc] init]; self.healthStore = [[HKHealthStore alloc] init];
if ([HKHealthStore isHealthDataAvailable]) { if ([HKHealthStore isHealthDataAvailable]) {
NSSet *writeDataTypes;
NSSet *writeDataTypes = [self dataTypesToWrite]; NSSet *readDataTypes;
NSSet *readDataTypes = [self dataTypesToRead];
// get permissions from input object provided by JS options argument // get permissions from input object provided by JS options argument
NSDictionary* permissions =[input objectForKey:@"permissions"]; NSDictionary* permissions =[input objectForKey:@"permissions"];
...@@ -89,13 +88,22 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock ...@@ -89,13 +88,22 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock
if(writePerms != nil) { if(writePerms != nil) {
writeDataTypes = writePerms; 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) { [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
if (!success) { 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); NSString *errMsg = [NSString stringWithFormat:@"Error with HealthKit authorization: %@", 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)]); NSLog(errMsg);
callback(@[RCTMakeError(errMsg, nil, nil)]);
return; return;
} else { } else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
...@@ -111,7 +119,13 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock ...@@ -111,7 +119,13 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock
- (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback - (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]);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment