Kaynağa Gözat

Merge pull request #74 from kmagiera/locationStatusWhenInUse

Allow location type to be specified in getPermissionStatus.
Yonah Forst 7 yıl önce
ebeveyn
işleme
09b14d4506

+ 5
- 5
ReactNativePermissions.js Dosyayı Görüntüle

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

+ 5
- 4
ReactNativePermissions.m Dosyayı Görüntüle

@@ -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 Dosyayı Görüntüle

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

+ 1
- 1
permissions/RNPLocation.h Dosyayı Görüntüle

@@ -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 Dosyayı Görüntüle

@@ -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