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

+ 5
- 4
ReactNativePermissions.m View File

@@ -66,15 +66,17 @@ RCT_EXPORT_METHOD(openSettings)
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 71
     NSString *status;
72 72
     
73 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 78
             break;
79
+        }
78 80
         case RNPTypeCamera:
79 81
             status = [RNPAudioVideo getStatus:@"video"];
80 82
             break;
@@ -139,7 +141,6 @@ RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json r
139 141
 
140 142
 }
141 143
 
142
-
143 144
 - (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
144 145
 {
145 146
     if (self.locationMgr == nil) {

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

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

+ 1
- 1
permissions/RNPLocation.h View File

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

+ 9
- 8
permissions/RNPLocation.m View File

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