Rahul Vyas
Rahul Vyas

Reputation: 28720

How to get user response when prompted for core location?

When I install my application on the iPhone it asks for the current location with the options "Don't allow" and "Ok" in an alert. How do I find out which option was chosen? I also want to show this option only once. If the user chooses to allow their current location to be found, I want the device to automatically get the location in the background.

Upvotes: 1

Views: 659

Answers (2)

Justin Gallagher
Justin Gallagher

Reputation: 3232

Your controller should implement the CLLocationManagerDelegate protocol. This defines two methods that you will need to implement:

  1. – locationManager:didUpdateToLocation:fromLocation:
    In this method you put your code to handle location updates.

  2. – locationManager:didFailWithError:
    In this method you put you code to handle the user denying your request, or updates failing.

Once the user allows you to use their location, they won't be prompted again unless they exit the app. There isn't a way to prevent the phone from prompting users each time they start up the app though.

Upvotes: 0

Vladimir
Vladimir

Reputation: 170829

If user denied access to Location service then CLLocationManager delegate method didFailWithError: gets called:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    if (error.code ==  kCLErrorDenied){
            // User denied access to location service       
    }
}

Upvotes: 7

Related Questions