nosuic
nosuic

Reputation: 1360

How to check if Location Services is On or not?

How can I check if the user has turned off Location Services ?

So that I can prompt him/her to turn it On in order to use my app.

Thank you !

Upvotes: 6

Views: 10236

Answers (4)

Teja Kumar Bethina
Teja Kumar Bethina

Reputation: 3726

Use the following code, which will work even in iOS 8.

if([CLLocationManager locationServicesEnabled]&&
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
  //...Location service is enabled
}
else
{
 //...Location service is disabled
}

Upvotes: 0

Felix
Felix

Reputation: 35394

The CLLocationManager provides class methods to determine the availability of location services:

- (BOOL)locationServicesEnabled (for < iOS 4.0)

+ (BOOL)locationServicesEnabled (for iOS 4.0 and greater)

+ (CLAuthorizationStatus)authorizationStatus (for iOS 4.2+)

(and others, see documentation)

Upvotes: 13

shankar
shankar

Reputation: 239

Use the below piece of code...

  if (![CLLocationManager locationServicesEnabled]) {


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled"
                                                    message:@"To re-enable, please go to Settings and turn on Location Service for this app."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];

}

Upvotes: 1

Anthony Williams
Anthony Williams

Reputation: 31

If your application absolutely cannot run without Location Services, then you can make Location Services a requirement for installing/running your app using the app's Info.plist. You do this by adding the UIDeviceCapabilities key to your app's Info.plist and giving it a corresponding value of "location-services" minus the quotes.

With the Info.plist configured this way, if Location Services are turned off, or if the device is in airplane mode, or anything else is preventing the use of Location Services on the device, iOS will prompt the user to turn on Location Services when the app is opened.

EDIT: Brief experimentation seems to indicate that iOS does not prompt the user in this circumstance, so this would not be a good solution for you.

For more information, you can look at the Information Property List Key Reference section of Apple's developer documentation.

Upvotes: 3

Related Questions