didUpdateLocations not called

江枫思渺然 提交于 2019-11-27 19:01:09
MarMass

Furthermore in iOS8 you must have two extra things:

  • 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]

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/

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.

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.

Gabriel.Massana

Make sure you added CLLocationManager as a property.

@property (nonatomic , strong) CLLocationManager *locationManager;

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];
}
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).

https://devforums.apple.com/message/1073267#1073267

Please note that in iOS 11 and later, a third key has to be supplied to your info.plist: NSLocationAlwaysAndWhenInUseUsageDescription

If CLLocationManagerDelegate is set, MapView Delegate is also set

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!