Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didUpdateLocations not being called

I have my object set as the locationManager's delegate, and the didChangeAuthorizationStatus method is called, which does this:

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

I also have this method, which never gets called following this:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    if locations.count == 0 {
        return
    }

    //Do stuff
}

Any thoughts as to why this might not be being called? I suppose the object being deallocated is an option, but then it'd also be deallocated by the time it hit the authorizationStatus method.

like image 604
Andrew Avatar asked Oct 03 '14 11:10

Andrew


2 Answers

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.

like image 141
timgcarlson Avatar answered Oct 15 '22 10:10

timgcarlson


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.

like image 6
ios_dev Avatar answered Oct 15 '22 10:10

ios_dev