How 开发者_如何学Pythoncan 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 !
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)
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.
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];
}
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
}
精彩评论