为什么我的CLLocationmanager委托没有被调用?
我没有在sim或设备上获得任何位置回调。我已经调用了这个代码:
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);
}
和两个顶部的日志语句
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
和
- (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?
配置/其他信息:
- I分配了locationManager,并将其保存在retain属性中。
- 我调用了startUpdatingLocation
- 我正在使用4.1 SDK
- 问题在于Sim& iPod-touch(第二代)& iPhone-3,全部运行4.1
- 位置通知允许在我的应用中(两者都在设置中指示,因为我在警报中点击了允许。)
- 之前我已成功使用过CLLocationManager(在许多运送应用程序中!)这对我来说是一个真正的拔毛器。
- 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.
谢谢!
哇!好的,我找到了。
事实证明,使 CLLocationManager
的方法之一不会触发位置回调是在非主线程中进行所有设置。当我将我的设置例程移动到 performSelectorOnMainThread
时,所有操作完全符合预期。
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.
真是个噩梦!
希望这个答案有助于其他人...
Hope this answer helps others...
编辑/澄清:
最初,我有这样的事情:
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];
}
但是调用 setupLocationManager
在一个线程中阻止了回调。所以我的修复是移动线
But calling setupLocationManager
in a thread prevented the callbacks. So my fix was to move the line
[self setupLocationManager];
退出线程并返回 appDidFinishLaunchingWithOptions