Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking location service permission on iOS

How can I check if location service is enabled for my app?

I have 2 storyboards and I want to check location service. If location service enabled for my app, I want to launch map storyboard with location. Otherwise, I want to launch another storyboard. How can I do programmatically?

like image 815
Melih Mucuk Avatar asked Mar 01 '13 07:03

Melih Mucuk


People also ask

How do you check if Location Services are enabled iOS?

The user can enable or disable location services from the Settings app by toggling the Location Services switch in General. You should check the return value of locationServiceEnabled() method before starting location updates to determine whether the user has location services enabled for the current device.

How do I check permissions in iOS?

Go to Settings > Privacy, then tap App Privacy Report (iOS 15.2 or later). The App Privacy Report shows you how apps are using the permissions you granted them and shows you their network activity.

Where is location permission in settings?

Tap App location permissions. Under ”Allowed all the time," “Allowed only while in use,” and “Not allowed,” find the apps that can use your phone's location. To change the app's permissions, tap it. Then, choose the location access for the app.

How do I know if I have Location Services?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.


2 Answers

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];     } } 
like image 181
Melih Mucuk Avatar answered Sep 16 '22 15:09

Melih Mucuk


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)   //Usage NSString* 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     } } 
like image 45
swiftBoy Avatar answered Sep 18 '22 15:09

swiftBoy