CLLocationManager startUpdatingLocation not calling locationManager:didUpdateLocations: or locationManager:didFailWithError:

前端 未结 8 1736
囚心锁ツ
囚心锁ツ 2020-12-05 06:21

I\'m trying to use the CLLocationManager framework in my iOS project to access the user\'s location but when I call

[locationManager startUpdat         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 07:25

    I had the following in my code base:

    -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
        if( status == kCLAuthorizationStatusAuthorizedAlways ) {
            _locationManager = [[CLLocationManager alloc] init];
            _locationManager.delegate = self;
            _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            _locationManager.distanceFilter = 200;
        }
    }
    

    Turns out that this was getting called in an infinite loop, because initializing a locationManager triggers this method for some reason? I had not realized that. I had put it there to catch when permissions were granted. Instead, I set up a location manager and started updating the location, but then this fired and replaced it with one that wasn't updating the location, and kept looping over and over.

    Solution for me was just to add && !_locationManager to the if condition.

提交回复
热议问题