Why is my CLLocationmanager delegate not getting called?

前端 未结 5 1576
梦谈多话
梦谈多话 2020-12-13 07:34

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

- (void)startLocationCallbacks: (NSObject*) ignore
{
  loc         


        
相关标签:
5条回答
  • 2020-12-13 07:39

    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

    0 讨论(0)
  • 2020-12-13 07:43

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

    0 讨论(0)
  • 2020-12-13 07:48

    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

    0 讨论(0)
  • 2020-12-13 07:48

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

    0 讨论(0)
  • 2020-12-13 07:55

    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

    0 讨论(0)
提交回复
热议问题