Browse Source

Allow location type to be specified in getPermissionStatus.

This change allows to get information whether location is authorised "whenInUse" or "always" on iOS.
Krzysztof Magiera 7 years ago
parent
commit
95884a0b5d

+ 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',
57
 	}
57
 	}
58
 
58
 
59
 
59
 
60
-	getPermissionStatus(permission) {
60
+	getPermissionStatus(permission, type) {
61
   	if (this.getPermissionTypes().indexOf(permission) >= 0) {
61
   	if (this.getPermissionTypes().indexOf(permission) >= 0) {
62
-			return RNPermissions.getPermissionStatus(permission)
62
+			return RNPermissions.getPermissionStatus(permission, type)
63
 		} else {
63
 		} else {
64
 			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
64
 			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
65
 		}
65
 		}
67
 
67
 
68
 	requestPermission(permission, type) {
68
 	requestPermission(permission, type) {
69
 		let options;
69
 		let options;
70
-		
70
+
71
 		if (this.getPermissionTypes().indexOf(permission) === -1) {
71
 		if (this.getPermissionTypes().indexOf(permission) === -1) {
72
 			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
72
 			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on ${Platform.OS}`)
73
 		} else if (permission == 'backgroundRefresh'){
73
 		} else if (permission == 'backgroundRefresh'){
89
 		function processNext() {
89
 		function processNext() {
90
 			i--
90
 			i--
91
 			let p = permissions[i]
91
 			let p = permissions[i]
92
-			
92
+
93
 			if (!p) {
93
 			if (!p) {
94
 				return Promise.resolve(obj)
94
 				return Promise.resolve(obj)
95
 			}
95
 			}

+ 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

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

+ 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