ios 未调用 didUpdateLocations
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19624719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
didUpdateLocations not called
提问by mikeholp
I'm trying to get my current location, but the break point in didUpdateLocations is never being called.
我正在尝试获取我当前的位置,但从未调用过 didUpdateLocations 中的断点。
LocationManager:
位置管理器:
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDesiredAccuracy:kCLDistanceFilterNone];
[locationManager startUpdatingLocation];
Delegate method:
委托方式:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
I confirmed that location services and enabled and authorized.
我确认了定位服务并启用和授权。
Why is the locationManager delegate method not being called like it should?
为什么 locationManager 委托方法没有像它应该的那样被调用?
Thanks, Mike
谢谢,迈克
回答by MarMass
Furthermore in iOS8 you must have two extra things:
此外,在 iOS8 中你必须有两个额外的东西:
Add a key to your
Info.plist
and request authorization from the location manager asking it to start.NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
You need to request authorization for the corresponding location method.
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
向您
Info.plist
的位置添加密钥并请求位置管理器的授权以启动。NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
您需要为相应的定位方法申请授权。
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
Code example:
代码示例:
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];
Source: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
来源:http: //nevan.net/2014/09/core-location-manager-changes-in-ios-8/
回答by Tim Bodeit
When I had this problem, it was due to a threading issue.
当我遇到这个问题时,是由于线程问题。
Make sure, that all of these methods are called on the main thread.
It is very important, that not only the startUpdatingLocation
method is called on the main thread, but the others as well.
确保所有这些方法都在主线程上调用。非常重要的是,不仅startUpdatingLocation
在主线程上调用该方法,还调用其他方法。
You can force code to be run on the main thread by wrapping it inside
您可以通过将代码包装在其中来强制在主线程上运行代码
dispatch_sync(dispatch_get_main_queue(), ^{
});
Also check out this answer.
也看看这个答案。
回答by Gabriel.Massana
Make sure you added CLLocationManager as a property.
确保您将 CLLocationManager 添加为属性。
@property (nonatomic , strong) CLLocationManager *locationManager;
回答by Zoltan Vinkler
Yes, the property was the solution for me, and good idea to check the Location Service is enabled:
是的,该物业是我的解决方案,检查定位服务是否启用是个好主意:
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}
回答by dreday
You do have to tell the simulator what location to simulate. If you don't specify a location your CLLocationManager
delegate methods will never get called. You can use the simulator menu Debug -> Location. Also in Xcode down by the debug area there'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's still not the same as the real device though).
您必须告诉模拟器要模拟的位置。如果您不指定位置,您的CLLocationManager
委托方法将永远不会被调用。您可以使用模拟器菜单 Debug -> Location。同样在调试区域的 Xcode 中,从 Xcode 运行应用程序时会出现一个小位置箭头。您可以使用它来指定 GPX 文件来模拟运动(尽管它仍然与真实设备不同)。
回答by Urvish Modi
Please note that in iOS 11 and later, a third key has to be supplied to your info.plist: NSLocationAlwaysAndWhenInUseUsageDescription
请注意,在 iOS 11 及更高版本中,必须向 info.plist 提供第三个键: NSLocationAlwaysAndWhenInUseUsageDescription
回答by Satyam Tayal
If CLLocationManagerDelegate is set, MapView Delegate is also set
如果设置了 CLLocationManagerDelegate,也会设置 MapView Delegate
Also Check the location of the simulator, Click on Simulator > Debug > Location, if it is none change into city run or freeway drive. Its worked for me.
还要检查模拟器的位置,单击 Simulator > Debug > Location,如果没有改变到 city run 或 freeway drive。它对我有用。