Checking location service permission on iOS Checking location service permission on iOS ios ios

Checking location service permission on iOS


This is the correct.

if ([CLLocationManager locationServicesEnabled]){    NSLog(@"Location Services Enabled");    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){        alert = [[UIAlertView alloc] initWithTitle:@"App Permission Denied"                                                message:@"To re-enable, please go to Settings and turn on Location Service for this app."                                           delegate:nil                                  cancelButtonTitle:@"OK"                                  otherButtonTitles:nil];        [alert show];    }}


Tested on iOS 9.2

For getting location updates we should always check

  • Location services enabled on user's iOS Device and
  • Location services enabled for particular app

and launching user on correct settings screen to enable

Launch iOS Device Location Settings page

Step.1 Go to Project settings --> Info --> URL Types --> Add New URL Schemes

enter image description here

Step.2 Use below code to launch direct phone's location settings page: (Note: The URL Scheme is different in iOS 10+, we check the version as stated here)

 #define SYSTEM_VERSION_LESS_THAN(v)  ([[[UIDevice  currentDevice] systemVersion] compare:v options:NSNumericSearch] ==  NSOrderedAscending) //UsageNSString* url = SYSTEM_VERSION_LESS_THAN(@"10.0") ? @"prefs:root=LOCATION_SERVICES" : @"App-Prefs:root=Privacy&path=LOCATION";            [[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];

enter image description here

Launch Application Location Settings page

Use below code to launch direct application's location settings page

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

enter image description here

Here is the full code example :

#define SYSTEM_VERSION_LESS_THAN(v)  ([[[UIDevice  currentDevice] systemVersion] compare:v options:NSNumericSearch] ==  NSOrderedAscending)CLLocationManager *locationManager;-(void) checkLocationServicesAndStartUpdates{    locationManager = [[CLLocationManager alloc] init];    locationManager.delegate = self;    locationManager.desiredAccuracy = kCLLocationAccuracyBest;    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])    {        [locationManager requestWhenInUseAuthorization];    }    //Checking authorization status    if (![CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)    {        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled!"                                                            message:@"Please enable Location Based Services for better results! We promise to keep your location private"                                                           delegate:self                                                  cancelButtonTitle:@"Settings"                                                  otherButtonTitles:@"Cancel", nil];        //TODO if user has not given permission to device        if (![CLLocationManager locationServicesEnabled])        {            alertView.tag = 100;        }        //TODO if user has not given permission to particular app        else        {            alertView.tag = 200;        }        [alertView show];        return;    }    else    {        //Location Services Enabled, let's start location updates        [locationManager startUpdatingLocation];    }}

Handle the user click respone, and launch correct location settings

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if(buttonIndex == 0)//Settings button pressed    {        if (alertView.tag == 100)        {            //This will open ios devices location settings            NSString* url = SYSTEM_VERSION_LESS_THAN(@"10.0") ? @"prefs:root=LOCATION_SERVICES" : @"App-Prefs:root=Privacy&path=LOCATION";            [[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];        }        else if (alertView.tag == 200)        {            //This will opne particular app location settings            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];        }    }    else if(buttonIndex == 1)//Cancel button pressed.    {        //TODO for cancel    }}


-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{    NSLog(@"%@",error.userInfo);    if([CLLocationManager locationServicesEnabled]){        NSLog(@"Location Services Enabled");        if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){         UIAlertView    *alert = [[UIAlertView alloc] initWithTitle:@"App Permission Denied"                                               message:@"To re-enable, please go to Settings and turn on Location Service for this app."                                              delegate:nil                                     cancelButtonTitle:@"OK"                                     otherButtonTitles:nil];            [alert show];        }    } }

Reason behind this, this method will call when your service will be disable the location service. this code is useful for me.