我正在尝试获取当前位置,但是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委托方法?
谢谢,迈克
7 回答
此外,在iOS8中,你必须有两件额外的东西:
为您的
Info.plist
添加一个密钥,并请求位置管理员授权其启动 .NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
您需要请求相应位置方法的授权 .
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
代码示例:
资料来源:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
当我遇到这个问题时,这是由于线程问题 .
确保在主线程上调用所有这些方法 . 非常重要的是,不仅在主线程上调用了
startUpdatingLocation
方法,而且还调用了其他方法 .您可以通过将代码包装在主线程上来强制代码在主线程上运行
另请查看this answer .
确保将CLLocationManager添加为属性 .
是的,该属性是我的解决方案,并且最好检查位置服务是否已启用:
您必须告诉模拟器要模拟的位置 . 如果您未指定位置,则永远不会调用
CLLocationManager
委托方法 . 您可以使用模拟器菜单Debug - > Location . 同样在调试区域的Xcode中,'s a little location arrow that appears when running the app from Xcode. You can use that to specify a GPX file to simulate motion (it'仍然与真实设备不同) .https://devforums.apple.com/message/1073267#1073267
如果设置了CLLocationManagerDelegate,则还会设置MapView Delegate
同时检查模拟器的位置,单击模拟器>调试>位置,如果没有更改为城市运行或高速公路驱动器 . 它对我有用 .
请注意,在iOS 11及更高版本中,必须向info.plist提供第三个密钥:NSLocationAlwaysAndWhenInUseUsageDescription