7
location = [[CLLocationManager alloc] init];
    location.desiredAccuracy = kCLLocationAccuracyBestForNavigation ;
    location.distanceFilter = 10 ;
    location.delegate=self;



    locationEnabledBool = [CLLocationManager locationServicesEnabled];

    if (locationEnabledBool ==NO) {
        UIAlertView *locationAlert = [[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];
        [locationAlert show];
        [locationAlert release];

    }
    else
        [location startUpdatingLocation];

The value of locationEnabledBool is always YES, independent of whether or not location services are enabled. Can any body help?

Mike Hay
  • 2,828
  • 21
  • 26
alekhine
  • 1,600
  • 4
  • 20
  • 45

2 Answers2

11

instead of

if (locationEnabledBool == NO) {
    //give error message
}

try

if ( [CLLocationManager locationServicesEnabled] ==NO || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
    //give error message
}

I found this on the link.

Detecting whether location services are enabled for my app

Community
  • 1
  • 1
alekhine
  • 1,600
  • 4
  • 20
  • 45
  • `if ( [CLLocationManager locationServicesEnabled] == NO && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) { //give error message }` a little typo in the first [ bracket ] and a && instead of || – kalafun Sep 30 '14 at 15:25
0

When you are testing this code, you will need to make sure that you test it on a device, and not just using the iOS Simulator.

Also, I suggest that you double check in the Settings on that device to make sure that Location Services, on the first page of the settings, says Off.

Mike Hay
  • 2,828
  • 21
  • 26
  • 1
    Thanks for your response. I was running the code on device and checked that Location Services says OFF in settings. instead of if (locationEnabledBool ==NO) if (locationEnabledBool ==NO || ( [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)) worked for me. – alekhine Sep 03 '11 at 07:28