37

I'm trying to get my current location, but the break point in didUpdateLocations is never being called.

LocationManager:

locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDesiredAccuracy:kCLDistanceFilterNone];
[locationManager startUpdatingLocation];

Delegate method:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

I confirmed that location services and enabled and authorized.

Why is the locationManager delegate method not being called like it should?

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
mikeholp
  • 965
  • 3
  • 11
  • 14
  • 1
    had the same problem a couple of minutes ago, I had not done the `[[CLLocationManager alloc] init];`. Here is the [link](http://stackoverflow.com/questions/5374216/didupdatelocation-method-never-called) that helped me – nupac Oct 29 '13 at 16:22
  • try to remove this line : `[locationManager setDesiredAccuracy:kCLDistanceFilterNone];` as you set setDesiredAccuracy twice, and make locationManager a property – Julian Jan 29 '14 at 10:44

8 Answers8

69

Furthermore in iOS8 you must have two extra things:

  • Add a key to your Info.plist and request authorization from the location manager asking it to start.

    • NSLocationWhenInUseUsageDescription

    • NSLocationAlwaysUsageDescription

  • You need to request authorization for the corresponding location method.

    • [self.locationManager requestWhenInUseAuthorization]

    • [self.locationManager requestAlwaysAuthorization]

Code example:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];

Source: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

Mihir Oza
  • 2,768
  • 3
  • 35
  • 61
MarMass
  • 5,755
  • 1
  • 18
  • 15
36

When I had this problem, it was due to a threading issue.

Make sure, that all of these methods are called on the main thread. It is very important, that not only the startUpdatingLocation method is called on the main thread, but the others as well.

You can force code to be run on the main thread by wrapping it inside

dispatch_sync(dispatch_get_main_queue(), ^{

});

Also check out this answer.

Community
  • 1
  • 1
Tim Bodeit
  • 9,673
  • 3
  • 27
  • 57
10

Make sure you added CLLocationManager as a property.

@property (nonatomic , strong) CLLocationManager *locationManager;
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
  • 1
    Looks like this helped me - otherwise I kept getting EXC_BAD_ACCESS errors. Thanks :) – JakeP Sep 11 '14 at 15:58
6

Yes, the property was the solution for me, and good idea to check the Location Service is enabled:

if ([CLLocationManager locationServicesEnabled]) {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
}
Zoltan Vinkler
  • 1,207
  • 1
  • 15
  • 20
4

If CLLocationManagerDelegate is set, MapView Delegate is also set

Also Check the location of the simulator, Click on Simulator > Debug > Location, if it is none change into city run or freeway drive. Its worked for me.

4

Please note that in iOS 11 and later, a third key has to be supplied to your info.plist: NSLocationAlwaysAndWhenInUseUsageDescription

Urvish Modi
  • 1,118
  • 10
  • 11
3

You do have to tell the simulator what location to simulate. If you don't specify a location your CLLocationManager delegate methods will never get called. You can use the simulator menu Debug -> Location. Also in Xcode down by the debug area there's a little location arrow that appears when running the app from Xcode. You can use that to specify a GPX file to simulate motion (it's still not the same as the real device though).

https://devforums.apple.com/message/1073267#1073267

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
dreday
  • 53
  • 5
0

My app started sometimes experiencing a multi-minute delay in the first call to didUpdateLocations after I had changed the desiredAccuracy attribute of my CLLocationManager to have a value of kCLLocationAccuracyKilometer:

myLocationManager.desiredAccuracy = kCLLocationAccuracyKilometer; 
[myLocationManager startUpdatingLocation]; // Occasional long delay

After changing it back to kCLLocationAccuracyBest, the call to didUpdateLocations once again started always occurring immediately:

myLocationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[myLocationManager startUpdatingLocation]; // Calls didUpdateLocations immediately
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170