Why is my CLLocationmanager delegate not getting called?

此生再无相见时 提交于 2019-11-28 17:35:19

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

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

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

shankar

// 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

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 :) )

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!