Jelajahi Sumber

call request authorization on main thread and ignore the first call for didChangeAuthorization

Radu Popovici 9 tahun lalu
induk
melakukan
6b1c0f1722
1 mengubah file dengan 28 tambahan dan 21 penghapusan
  1. 28 21
      permissions/RNPLocation.m

+ 28 - 21
permissions/RNPLocation.m

@@ -37,32 +37,39 @@
 {
     NSString *status = [RNPLocation getStatus];
     if (status == RNPStatusUndetermined) {
-        self.completionHandler = completionHandler;
-        
-        self.locationManager = [[CLLocationManager alloc] init];
-        self.locationManager.delegate = self;
-        if ([type isEqualToString:@"always"]) {
-            [self.locationManager requestAlwaysAuthorization];
-        } else {
-            [self.locationManager requestWhenInUseAuthorization];
-        }
+        dispatch_async(dispatch_get_main_queue(), ^(){
+            self.completionHandler = completionHandler;
+            
+            self.locationManager = [[CLLocationManager alloc] init];
+            self.locationManager.delegate = self;
+                
+            if ([type isEqualToString:@"always"]) {
+                [self.locationManager requestAlwaysAuthorization];
+            } else {
+                [self.locationManager requestWhenInUseAuthorization];
+            }
+        });
     } else {
         completionHandler(status);
     }
 }
 
--(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
-    if (self.locationManager) {
-        self.locationManager.delegate = nil;
-        self.locationManager = nil;
-    }
-    
-    if (self.completionHandler) {
-        //for some reason, checking permission right away returns denied. need to wait a tiny bit
-        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
-            self.completionHandler([RNPLocation getStatus]);
-            self.completionHandler = nil;
-        });
+-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
+{
+    if (status != kCLAuthorizationStatusNotDetermined) {
+        if (self.locationManager) {
+            self.locationManager.delegate = nil;
+            self.locationManager = nil;
+        }
+
+        if (self.completionHandler) {
+            //for some reason, checking permission right away returns denied. need to wait a tiny bit
+            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
+                self.completionHandler([RNPLocation getStatus]);
+                self.completionHandler = nil;
+            });
+        }
     }
 }
+
 @end