13

I know the property locationServicesEnabled is deprecated in iOS 4. Instead, I should call locationServicesEnabled

In my app delegate method

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Some Code Here...
    CLLocationManager *manager = [[CLLocationManager alloc] init];
    if (![manager locationServicesEnabled])
    {   //show an alert
    }
 }   

I called the method, however, Xcode showed me a warning "locationServicesEnabled is deprecated". Anyone knows how to fix this? Because of the warning, if I turned off the location service in system preference, the alert view can't show.

Thanks!

Jing
  • 1,935
  • 3
  • 17
  • 26

4 Answers4

52

From the documentation:

locationServicesEnabled: A Boolean value indicating whether location services are enabled on the device. (read-only) (Deprecated in iOS 4.0. Use the locationServicesEnabled class method instead.)

So instead of [manager locationServicesEnabled] you should be using [CLLocationManager locationServicesEnabled]

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
  • 6
    BTW, it would have been a lot faster for you if you had looked this up in the documentation. It took me about a minute to find this information. – EmilioPelaez Aug 23 '11 at 04:16
  • 1
    Thanks, this works. But it seems only when all location services are disabled, the alert then show. I wonder if there is any way to show a alert whenever you turn off the location service for this app rather than turn off the whole location service? – Jing Aug 23 '11 at 04:54
  • Does this mean that if `locationServicesEnabled` returns `false`, the gps is `OFF` and if it returns `true` than gps is `ON`?? is this true?? – DShah Jul 05 '12 at 13:53
12

If you only want to know if your application has rights for use GPS you need to check [CLLocationManager authorizationStatus]

You can see this class method in the CLLocationManager Class Reference.

http://developer.apple.com/library/ios/#DOCUMENTATION/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

José
  • 136
  • 1
  • 2
  • 1
    +1 because this is the proper way to check for your app specifically. The locationServicesEnabled class will only tell you about the global switch, it seems.. Ive been playing around with these for hours. – pnizzle Aug 10 '12 at 05:47
  • @prinzzle [CLLocationManager authorizationStatus] not working in simulator 5.1... but its working in simulator 6.0... – Karthik Apr 29 '13 at 06:10
0

If you are developing an app to iOS 4 or higher you can /comment/ or delete like this:

- (BOOL)isLocationServicesEnabled
{
    BOOL locationServicesEnabledClassPropertyAvailable = [CLLocationManager respondsToSelector:@selector(locationServicesEnabled)];// iOS 4.x
    if (locationServicesEnabledClassPropertyAvailable) { // iOS 4.x
        return [CLLocationManager locationServicesEnabled];
    } else {
        return NO;
    }
}
andrewsi
  • 10,807
  • 132
  • 35
  • 51
Marc
  • 1
0

So you just need to remove [(id)self.locationManager headingAvailable] and replace it with [CLLocationManager locationServicesEnabled] problem solved...

Grd Peter
  • 3
  • 4