RequestWhenInUseAuthorization in iOS 8 and wait for user response

I am trying to find the user's location using CLLocationManager

. Here's 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 problem is the code keeps running, even the warning is on top. I want to stop execution until the user clicks allow or deny before updating the location. How can i do this?

I don't want to use a delay or timer.

+3


source to share


1 answer


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



+6


source







All Articles