didUpdateLocations未被调用
我试图得到我当前的位置,但didUpdateLocations中的断点永远不会被调用。
的LocationManager:
locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [locationManager setDesiredAccuracy:kCLDistanceFilterNone]; [locationManager startUpdatingLocation];
代表方法:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
我确认位置服务并启用和授权。
为什么不调用locationManager委托方法?
谢谢,迈克
另外在iOS8中,你必须有两件事:
-
将一个密钥添加到
Info.plist
并请求位置pipe理器的授权,要求它开始。-
NSLocationWhenInUseUsageDescription
-
NSLocationAlwaysUsageDescription
-
-
您需要为相应的位置方法请求授权。
-
[self.locationManager requestWhenInUseAuthorization]
-
[self.locationManager requestAlwaysAuthorization]
-
代码示例:
self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7. if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [self.locationManager requestWhenInUseAuthorization]; } [self.locationManager startUpdatingLocation];
来源: http : //nevan.net/2014/09/core-location-manager-changes-in-ios-8/
当我遇到这个问题时,这是由于线程问题。
确保所有这些方法都在主线程中调用。 非常重要的是,不仅在主线程上调用startUpdatingLocation
方法,还有其他方法。
您可以强制代码在主线程中运行,方法是将其包装在内部
dispatch_sync(dispatch_get_main_queue(), ^{ });
也检查出这个答案 。
确保你添加了CLLocationManager作为一个属性。
@property (nonatomic , strong) CLLocationManager *locationManager;
是的,该物业是我的解决scheme,并启用检查定位服务的好主意:
if ([CLLocationManager locationServicesEnabled]) { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; [self.locationManager startUpdatingLocation]; }
你必须告诉模拟器模拟什么位置。 如果你不指定一个位置,你的CLLocationManager
委托方法将永远不会被调用。 您可以使用模拟器菜单Debug – > Location。 另外,在Xcode下的debugging区域,从Xcode运行应用程序时会出现一个小小的位置箭头。 你可以使用它来指定一个GPX文件来模拟运动(尽pipe如此,它仍然不像真实的设备)。
如果CLLocationManagerDelegate被设置,MapView委托也被设置
另外检查模拟器的位置,点击模拟器>debugging>位置,如果没有改变到城市运行或高速公路驱动器。 它为我工作。