Search code examples
iphonemapkitcllocationmanagerlocationmanager

locationServicesEnabled in iOS >4.0 always showing UIAlert


I try do get the locationServicesEnabled function to work... but my UIAlert shows up no matter if the locationServices is enabled or not! How do I get this to work properly?

 @synthesize locationServicesEnabled
    -(void)viewDidLoad {
     [super viewDidLoad];

     if(![CLLocationManager locationServicesEnabled]) {
      self.locationManager = [[[CLLocationManager alloc] init] autorelease];
      locationManager.delegate = self;
      locationManager.desiredAccuracy = kCLLocationAccuracyBest;
      [locationManager startUpdatingLocation];
        } else {
            [[[[UIAlertView alloc] initWithTitle:@"Location services." 
                                         message:@"Location services are disabled." 
                                        delegate:nil 
                               cancelButtonTitle:@"OK" 
                               otherButtonTitles:nil] autorelease] show];       
        }
    }

Thank you in advance!


Solution

  • Looks like your condition is backwards. It currently says "if NOT location-services-enabled then start updating else alert".

    Change the if to:

    if([CLLocationManager locationServicesEnabled])
    

    Remove the !.