Search code examples
iphonecllocationmanager

Why is my CLLocationmanager delegate not getting called?


I'm not getting any location callbacks on either sim or device. I've got this code being called:

- (void)startLocationCallbacks: (NSObject*) ignore
{
  locationManager.delegate = self;
  locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
  locationManager.distanceFilter = MINIMUM_METERS;

  [locationManager startUpdatingLocation];
  NSLog(@"[DEBUG] [locationManager startUpdatingLocation] (%@, %@)", locationManager, locationManager.delegate);
}

and log statements at the top of both

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

and

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

but neither log statement ever gets called. Location Notifications are enabled for my app (as shown in Settings, plus I said "allow.")

What are some possible reasons that I'm not getting location updates?

Config/other info:

  • I have allocated locationManager, and saved it in a retain property.
  • I have called startUpdatingLocation
  • I'm using 4.1 SDK
  • Problem is on both Sim & iPod-touch (2nd Gen) & iPhone-3, all running 4.1
  • Location notifications are allowed in my app (both as indicated in Settings and because I clicked "allow" in the alert.)
  • I've used CLLocationManager successfully before (in many shipping apps!) This is a real hair-puller for me.

Thanks!


Solution

  • Whew! Ok, I found it.

    It turns out that one of the ways to make a CLLocationManager not fire off location callbacks is to do all the set-up in not-the-main-thread. When I moved my setup routine to a performSelectorOnMainThread, all worked exactly as expected.

    What a nightmare!

    Hope this answer helps others...

    Edit/clarification:

    Originally, I had something like this:

    - (BOOL) appDidFinishLaunchingWithOptions: (NSDictionary*) options
    {
         // ...[app setup, snip]
        [NSThread detachNewThreadSelector: @selector(postLaunchSetupThread) toTarget: self withObject: nil];
    }
    
    - (void)postLaunchSetupThread
    {
        NSAutoreleasePool *pool = [NSAutoreleasePool new];
        // ...[other setup, snip]
        [self setupLocationManager];
        [pool release];
    }
    
    - (void)setupLocationManager
    {
         self.myLocationManager = [[[CLLocationManager alloc] init] autorelease];
         [myLocationManager startLocationUpdates];
    }
    

    But calling setupLocationManager in a thread prevented the callbacks. So my fix was to move the line

    [self setupLocationManager];
    

    out of the thread and back into appDidFinishLaunchingWithOptions