Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
402 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...