22

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!

Olie
  • 24,597
  • 18
  • 99
  • 131
  • NOTE: I've seen several other similar SO questions, they didn't help. The whole "turn on your wifi", "don't use sim", etc., thing doesn't apply, here. Not that I can tell, anyway. I'm hoping to find a list of all the reasons I might not get the callbacks, so I can check them off. Thanks! – Olie Sep 17 '10 at 00:32
  • Have you tried to test if the delegate is getting notified? Run `[locationManager.delegate locationManager:locationManager didUpdateToLocation:newLocation fromLocation:oldLocation];` If by running this you get your delegate notified as expected, it would mean that your devices aren't able to get a location at all. If the delegate doesn't get notified, it may mean a bug on you code with the delegate object or a bug on the SDK (very very improbable). – vfn Sep 17 '10 at 01:56
  • 2
    Another clue: I turned OFF location notifications in Settings, trying to get it to ask me again. No ask. Also, no "user denied" error (didFailWithError:...) It's really just as if my location manager just isn't sparking. Or not recognizing me as the delegate. When I inspect locationManager.delegate, it's the right thing. Grr, mutter, grumble! – Olie Sep 17 '10 at 02:35
  • Have you tried to fire the delegated method to see if the delegate will be notified, as I suggested on the previous comment? – vfn Sep 17 '10 at 02:57
  • 1
    @vfn: Tried your trick (hooked that code to a test button), my callback gets called & does what is expected. So now the big question is: what can trigger my location manager to *not* send me either valid locations OR errors? (*I.e.*, neither my didUpdateLocation nor my didFailWithError routines are called.) – Olie Sep 17 '10 at 03:00
  • If your test worked, it means that your delegate is really valid, and so what is happening is that your device is not receiving any new location it seems that the GPS is not working (at least within your app). – vfn Sep 17 '10 at 03:10
  • 1
    Change the accuracy to best and remove the distance filter. – vfn Sep 17 '10 at 03:11
  • Possible duplicate of [Why the CLLocationManager delegate is not getting called in iPhone SDK 4.0?](http://stackoverflow.com/questions/3058927/why-the-cllocationmanager-delegate-is-not-getting-called-in-iphone-sdk-4-0) – Max MacLeod Nov 12 '15 at 10:06
  • 1
    @MaxMacLeod: similar symptoms, but with a different root cause. That question is more properly "Why isn't my released variable continuing to function?" whereas this one is more like "why isn't my action that I put on a thread that never gets any time never getting any time?" :) – Olie Nov 12 '15 at 15:40

5 Answers5

54

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

Olie
  • 24,597
  • 18
  • 99
  • 131
  • 5
    Thanks for this - just saved me hours of madness trying to figure it out. CLLocationManager indeed doesn't like being created off the main thread. – lxt Feb 14 '11 at 10:25
  • 4
    I was going crazy trying to figure this out! thank you so much! – mota Feb 02 '12 at 08:08
  • 2
    You're a life saver! Spent all day pulling my hairs on this one... Thanks! – Alex Zak Mar 14 '12 at 00:51
  • 1
    the main thread!! works! thanks. Banged my head on this one for 30 minutes. This fixed my problem too. – zumzum Oct 06 '14 at 03:10
17

Actually you can run it from another thread as well. From Apple documentation:

Configuration of your location manager object must always occur on a thread with an active run loop, such as your application’s main thread.

Just make sure your run loop is running on that thread, and the CLLocationManager events are dispatched. More about run loop: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html

ttekin
  • 827
  • 8
  • 9
  • True (+1), I should have mentioned that in my answer -- but the point is: you can't set up location manager is a not-running thread and expect it to perform callbacks. Solutions include (a) don't set it up in a separate thread and (b) make sure that your separate thread's runloop is running. – Olie Feb 22 '12 at 18:57
  • 4
    I've attempted to get this to work on a thread with a running run loop and the delegate methods still weren't called. Can you provide a working example implementation? – Mark Amery Oct 30 '13 at 12:37
1

For iOS 8

1) I placed these lines right after I init'd the location manager.

if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [self.locationManager requestAlwaysAuthorization];
}

2) I added NSLocationAlwaysUsageDescription to the plist and set it to a string and added a message. 3) In my target, I clicked on Capabilities tab, and then turned on Background Modes and checked "Location Updates" and "Uses Bluetooth LE accessories"

This worked for me

rharvey
  • 1,987
  • 1
  • 28
  • 23
0

// check out this //...CLLocationManager does n't call delegate method properly

//...after lot of R&D I'm using a watchdog method it calls "

https://stackoverflow.com/questions/7156920/didupdatetolocation-doesnt-gets-called-immediately/14605889#14605889

Community
  • 1
  • 1
shankar
  • 239
  • 3
  • 5
  • This is a different case, the question was about the delegate not getting called at all. The link u provided is about timing. Olie has the right answer – Has AlTaiar Dec 04 '13 at 01:24
0

In my case it was because of my device was unable to determine location - while staying indoors GPS signal was unavailable and wi-fi was also disconnected so location manager just wasn't able to receive any location data and delegate was never called.

Once I've connected wi-fi it worked (I guess moving outdoors should also do the trick in that case, but sometimes it is not very convenient way :) )

vir us
  • 9,920
  • 6
  • 57
  • 66