Abhinav
Abhinav

Reputation: 38142

Location service iOS alert call back

When we use location services in an application, we receive an iOS alert saying the application is trying to use the current location -- Allow/Don't Allow.

Do we have a delegate call back for these buttons?

I want to handle tap on "Don't Allow".

Upvotes: 8

Views: 10624

Answers (3)

TharakaNirmana
TharakaNirmana

Reputation: 10353

You can simply get the action selected like below:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [self addRegion];
    }
    else if (status == kCLAuthorizationStatusDenied) {
        NSLog(@"Location access denied");
    }
}

make sure to set the delegate of location manager.

Upvotes: 7

nevan king
nevan king

Reputation: 113747

You should also check to see if the user has allowed location services for your app before starting the location manager. Use the CLLocationManager class method locationServicesEnabled to check.

Here's the doc:

locationServicesEnabled

Returns a Boolean value indicating whether location services are enabled on the device.

+ (BOOL)locationServicesEnabled

Return Value YES if location services are enabled or NO if they are not.

Discussion The user can enable or disable location services altogether from the Settings application by toggling the switch in Settings > General > Location Services.

You should check the return value of this method before starting location updates to determine if the user has location services enabled for the current device. If this method returns NO and you start location updates anyway, the Core Location framework prompts the user with a confirmation panel asking whether location services should be reenabled.

Upvotes: 3

Darren
Darren

Reputation: 25619

You don't have direct access to that alert.

If the user presses "Don't Allow", or if the app otherwise doesn't have permission to use location services then CLLocationManager will call locationManager:didFailWithError: on its delegate. The error domain will be kCLErrorDomain and the error code will be kCLErrorDenied.

Upvotes: 17

Related Questions