我不会在sim或设备上得到任何位置回调。我已经调用了这个代码:
- (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但是日志语句都没有被调用。我的应用程序启用了位置通知(如“设置”中所示,加上我说“允许”)。
我没有得到位置更新的可能原因是什么?
Config/其他信息:
我已经分配了startUpdatingLocation
H 116我以前成功地使用过CLLocationManager (在许多航运应用中!)这对我来说是个真正的发型师。--
谢谢!
发布于 2010-09-17 05:48:40
呼!好吧,我找到了。
结果表明,使CLLocationManager不触发位置回调的方法之一是在非主线程中执行所有设置。当我将我的设置例程移到一个performSelectorOnMainThread时,所有这些都完全按照预期工作。
真是一场噩梦!
希望这个答案能帮助别人..。
Edit/clarification:
最初,我有这样的事情:
- (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阻止了回调。所以我的解决办法是移动线
[self setupLocationManager];退出线程并返回到appDidFinishLaunchingWithOptions中
发布于 2012-02-11 01:58:51
实际上,您也可以从另一个线程运行它。来自苹果公司的文档:
位置管理器对象的
配置必须始终发生在具有活动运行循环的线程上,例如应用程序的主线程。
只需确保您的run循环在该线程上运行,并且分派了CLLocationManager事件。更多关于run循环的信息:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html
发布于 2015-01-22 12:54:16
对于iOS 8
1)我把这些线放在我插入位置经理之后。
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}2)我将NSLocationAlwaysUsageDescription添加到plist中,并将其设置为字符串并添加了一条消息。3)在我的目标中,我点击了“功能”选项卡,然后打开背景模式,并检查了“位置更新”和“使用蓝牙LE附件”。
这对我来说很管用
https://stackoverflow.com/questions/3731881
复制相似问题