How can I check if the user has allowed location for mu app?
Normally I would use authorizationStatus
method of the CLLocationManager
class, but it is only available in iOS 4.2 and newer. Is it possible to achieve this somehow while still using SDK 4.2, so that the app can still run on devices with older versions of iOS, or do I have to downgrade the SDK?
And along the same line, I need a similar alternative for the locationServicesEnabled
method prior to iOS 4.0.
Asked
Active
Viewed 6.0k times
36
3 Answers
60
When you call -startUpdatingLocation
, if location services were denied by the user, the location manager delegate will receive a call to -locationManager:didFailWithError:
with the kCLErrorDenied
error code. This works both in all versions of iOS.

doctorBroctor
- 2,018
- 1
- 14
- 17

Martin Gordon
- 36,329
- 7
- 58
- 54
-
18I like calling `[CLLocationManager locationServicesEnabled]` as described below better. That way you can prevent unnecessary calls. – SpacyRicochet Dec 08 '12 at 15:24
-
7For users who stumble here without the 'prior iOS 4.2' constraint, the answer is: [CLLocationManager authorizationStatus] – fionbio Feb 02 '13 at 23:06
-
31@SpacyRicochet does this answer the question though? OP asked how to determine if location services enabled for a particular app and `[CLLocationManager locationServicesEnabled]` just tests for device wide location services. `[CLLocationManager authorizationStatus]` tests for app specific authorization. – NSTJ Feb 23 '13 at 13:12
-
1@SpacyRicochet, [CLLocationManager locationServicesEnabled] only checks the general "Location Services" switch, it does not check the single app's status. – LiangWang Jun 17 '14 at 01:01
-
@nous Now it's `kCLAuthorizationStatusDenied` – dclowd9901 Dec 27 '14 at 18:07
37
I've combined two techniques in the code below
MKUserLocation *userLocation = map.userLocation;
BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
BOOL locationAvailable = userLocation.location!=nil;
if (locationAllowed==NO) {
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];
} else {
if (locationAvailable==NO)
[self.map.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
}

EasyCoder
- 523
- 4
- 6
-
Same problem as above. Please do not assume that a nil user location corresponds to no authorization because that might not be true. – millenomi Jan 12 '13 at 23:27
-
7I think this is not so correct. [CLLocationManager locationServicesEnabled] tells you that Location is On or Off to all apps and [CLLocationManager authorizationStatus] tells if app has authorization to check location. So UIAlertView should say to turn location on and not to authorize app. But it was useful, thank you @EasyCoder – Mário Carvalho Feb 28 '13 at 14:30
7
Hmm.. I didn't think to use authorizationStatus
or locationServicesEnabled
. What I did was the following:
MKUserLocation *userLocation = mapView.userLocation;
if (!userLocation.location) {
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];
return;
}
I'm glad to see there's a better method to check, but I haven't had any problems with the way I detect if my app is allowed to use the GPS.
Hope this helps!

donkim
- 13,119
- 3
- 42
- 47
-
1Note that the user location may not be available even though location services are enabled. For example, the user may be outside of coverage for all sensors (e.g. an iPod touch with no Wi-Fi on). – millenomi Jan 12 '13 at 23:27