didUpdateLocations not being called

折月煮酒 提交于 2019-12-04 10:12:49

This is way late for an answer, but I stumbled across this when I was having the same issue (didChangeAuthorizationStatus was being called but not didUpdateLocations). Well of course it's something not code related, but rather I was testing in the simulator which did not have a location set. Thus the location was never found, resulting in didUpdateLocations never being called.

To fix this... in the simulator go to Debug -> Location-> <choose location>.

EDIT It should also be noted that locationManager:didFailWithError: will run if the location is not set in the simulator, as you'd expect.

It's working in iOS 10 and iOS 11 as well,

Create property of CLLocationManager using strong and add CLLocationManagerDelegate

@property (strong, nonatomic) CLLocationManager *locationManager;

Add below properties into your info.plist file

Privacy - Location When In Use Usage Description
Privacy - Location Always Usage Description
Privacy - Location Usage Description

Add below method in to your .m file

-(void)getCurrentLocation
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate=self;
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    self.locationManager.distanceFilter=kCLDistanceFilterNone;
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startMonitoringSignificantLocationChanges];
    [self.locationManager startUpdatingLocation];
}

and just call [self getCurrentLocation] method.

Here is a snippet from my app:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    ...
    locationManager.delegate = self
    locationManager.activityType = CLActivityType.Fitness
    locationManager.distanceFilter = 10   // 10m
    locationManager.requestAlwaysAuthorization()

    // get current location
    if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
        locationManager.startUpdatingLocation()
    }
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .Authorized {
        locationManager.startUpdatingLocation()
    }
}

You have to put the NSLocationAlwaysUsageDescription key in your Info.plist and set the value to the authorization question. This is a requirement as of iOS 8.

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