Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestWhenInUseAuthorization in iOS 8 and wait until user responds

I am trying to fetch user's location using CLLocationManager. Here is my simple code:

if(IS_OS_8_OR_LATER) {
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        // iOS8+
        // Sending a message to avoid compile time error
        [[UIApplication sharedApplication] sendAction:@selector(requestWhenInUseAuthorization)
                                                   to:self.locationManager
                                                 from:self
                                             forEvent:nil];
        CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

        if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
            authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
            authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

            [self.locationManager startUpdatingLocation];
        }else{
            [self removeIndicatorView];
            return;
        }
    }
}

My issue is, the code keep executing even the alert is on top. I want to stop the execution until user press allow or deny before updating location. How I can do it?

I don't want to use delay or timer.

like image 764
iBug Avatar asked Mar 18 '23 01:03

iBug


1 Answers

Use the delegate method locationManager:didChangeAuthorizationStatus: to run your code when the user allows or disallows the location request.

like image 114
Enrico Susatyo Avatar answered Apr 29 '23 01:04

Enrico Susatyo