Browse Source

Merge pull request #74 from kmagiera/locationStatusWhenInUse

Allow location type to be specified in getPermissionStatus.
Yonah Forst 7 years ago
parent
commit
09b14d4506

+ 5
- 5
ReactNativePermissions.js View File

15
 		'reminder',
15
 		'reminder',
16
 		'bluetooth',
16
 		'bluetooth',
17
 		'notification',
17
 		'notification',
18
-		'backgroundRefresh', 
18
+		'backgroundRefresh',
19
 	],
19
 	],
20
 	android: [
20
 	android: [
21
 		'location',
21
 		'location',
58
 	}
58
 	}
59
 
59
 
60
 
60
 
61
-	getPermissionStatus(permission) {
61
+	getPermissionStatus(permission, type) {
62
   	if (this.getPermissionTypes().indexOf(permission) >= 0) {
62
   	if (this.getPermissionTypes().indexOf(permission) >= 0) {
63
-			return RNPermissions.getPermissionStatus(permission)
63
+			return RNPermissions.getPermissionStatus(permission, type)
64
 		} else {
64
 		} else {
65
 			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
65
 			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
66
 		}
66
 		}
68
 
68
 
69
 	requestPermission(permission, type) {
69
 	requestPermission(permission, type) {
70
 		let options;
70
 		let options;
71
-		
71
+
72
 		if (this.getPermissionTypes().indexOf(permission) === -1) {
72
 		if (this.getPermissionTypes().indexOf(permission) === -1) {
73
 			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
73
 			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
74
 		} else if (permission == 'backgroundRefresh'){
74
 		} else if (permission == 'backgroundRefresh'){
90
 		function processNext() {
90
 		function processNext() {
91
 			i--
91
 			i--
92
 			let p = permissions[i]
92
 			let p = permissions[i]
93
-			
93
+
94
 			if (!p) {
94
 			if (!p) {
95
 				return Promise.resolve(obj)
95
 				return Promise.resolve(obj)
96
 			}
96
 			}

+ 5
- 4
ReactNativePermissions.m View File

66
     }
66
     }
67
 }
67
 }
68
 
68
 
69
-RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
69
+RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
70
 {
70
 {
71
     NSString *status;
71
     NSString *status;
72
     
72
     
73
     switch (type) {
73
     switch (type) {
74
             
74
             
75
-        case RNPTypeLocation:
76
-            status = [RNPLocation getStatus];
75
+        case RNPTypeLocation: {
76
+            NSString *locationPermissionType = [RCTConvert NSString:json];
77
+            status = [RNPLocation getStatusForType:locationPermissionType];
77
             break;
78
             break;
79
+        }
78
         case RNPTypeCamera:
80
         case RNPTypeCamera:
79
             status = [RNPAudioVideo getStatus:@"video"];
81
             status = [RNPAudioVideo getStatus:@"video"];
80
             break;
82
             break;
139
 
141
 
140
 }
142
 }
141
 
143
 
142
-
143
 - (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
144
 - (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
144
 {
145
 {
145
     if (self.locationMgr == nil) {
146
     if (self.locationMgr == nil) {

+ 2
- 2
android/src/main/java/com/joshblour/reactnativepermissions/ReactNativePermissionsModule.java View File

46
   }
46
   }
47
 
47
 
48
   @ReactMethod
48
   @ReactMethod
49
-  public void getPermissionStatus(String permissionString, Promise promise) {
49
+  public void getPermissionStatus(String permissionString, String nullForiOSCompat, Promise promise) {
50
     String permission = permissionForString(permissionString);
50
     String permission = permissionForString(permissionString);
51
 
51
 
52
     // check if permission is valid
52
     // check if permission is valid
94
         // NOOP
94
         // NOOP
95
       }
95
       }
96
     };
96
     };
97
-    
97
+
98
     mPermissionsModule.requestPermission(permission, new PromiseImpl(resolve, reject));
98
     mPermissionsModule.requestPermission(permission, new PromiseImpl(resolve, reject));
99
   }
99
   }
100
 
100
 

+ 1
- 1
permissions/RNPLocation.h View File

11
 
11
 
12
 @interface RNPLocation : NSObject
12
 @interface RNPLocation : NSObject
13
 
13
 
14
-+ (NSString *)getStatus;
14
++ (NSString *)getStatusForType:(NSString *)type;
15
 - (void)request:(NSString *)type completionHandler:(void (^)(NSString *))completionHandler;
15
 - (void)request:(NSString *)type completionHandler:(void (^)(NSString *))completionHandler;
16
 
16
 
17
 @end
17
 @end

+ 9
- 8
permissions/RNPLocation.m View File

17
 
17
 
18
 @implementation RNPLocation
18
 @implementation RNPLocation
19
 
19
 
20
-+ (NSString *)getStatus
20
++ (NSString *)getStatusForType:(NSString *)type
21
 {
21
 {
22
     int status = [CLLocationManager authorizationStatus];
22
     int status = [CLLocationManager authorizationStatus];
23
     switch (status) {
23
     switch (status) {
24
         case kCLAuthorizationStatusAuthorizedAlways:
24
         case kCLAuthorizationStatusAuthorizedAlways:
25
-        case kCLAuthorizationStatusAuthorizedWhenInUse:
26
             return RNPStatusAuthorized;
25
             return RNPStatusAuthorized;
26
+        case kCLAuthorizationStatusAuthorizedWhenInUse:
27
+            return [type isEqualToString:@"always"] ? RNPStatusDenied : RNPStatusAuthorized;
27
         case kCLAuthorizationStatusDenied:
28
         case kCLAuthorizationStatusDenied:
28
             return RNPStatusDenied;
29
             return RNPStatusDenied;
29
         case kCLAuthorizationStatusRestricted:
30
         case kCLAuthorizationStatusRestricted:
35
 
36
 
36
 - (void)request:(NSString*)type completionHandler:(void (^)(NSString *))completionHandler
37
 - (void)request:(NSString*)type completionHandler:(void (^)(NSString *))completionHandler
37
 {
38
 {
38
-    NSString *status = [RNPLocation getStatus];
39
+    NSString *status = [RNPLocation getStatusForType:nil];
39
     if (status == RNPStatusUndetermined) {
40
     if (status == RNPStatusUndetermined) {
40
         self.completionHandler = completionHandler;
41
         self.completionHandler = completionHandler;
41
-        
42
+
42
         if (self.locationManager == nil) {
43
         if (self.locationManager == nil) {
43
             self.locationManager = [[CLLocationManager alloc] init];
44
             self.locationManager = [[CLLocationManager alloc] init];
44
             self.locationManager.delegate = self;
45
             self.locationManager.delegate = self;
45
         }
46
         }
46
-        
47
+
47
         if ([type isEqualToString:@"always"]) {
48
         if ([type isEqualToString:@"always"]) {
48
             [self.locationManager requestAlwaysAuthorization];
49
             [self.locationManager requestAlwaysAuthorization];
49
         } else {
50
         } else {
64
             self.locationManager.delegate = nil;
65
             self.locationManager.delegate = nil;
65
             self.locationManager = nil;
66
             self.locationManager = nil;
66
         }
67
         }
67
-        
68
+
68
         if (self.completionHandler) {
69
         if (self.completionHandler) {
69
             //for some reason, checking permission right away returns denied. need to wait a tiny bit
70
             //for some reason, checking permission right away returns denied. need to wait a tiny bit
70
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
71
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
71
-                self.completionHandler([RNPLocation getStatus]);
72
+                self.completionHandler([RNPLocation getStatusForType:nil]);
72
                 self.completionHandler = nil;
73
                 self.completionHandler = nil;
73
             });
74
             });
74
-        }        
75
+        }
75
     }
76
     }
76
 }
77
 }
77
 @end
78
 @end