Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my CLLocationmanager delegate not getting called?

Tags:

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!

like image 824
Olie Avatar asked Sep 17 '10 00:09

Olie


People also ask

What is CLLocationManagerDelegate?

The methods that you use to receive events from an associated location-manager object. iOS 2.0+ iPadOS 2.0+ macOS 10.6+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+


1 Answers

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

like image 175
Olie Avatar answered Sep 30 '22 15:09

Olie