1
votes

I have an app that shows some data based on user's location. And it had been working fine with Swift2 and iOS9. Now none of the CLLocationManager delegate's functions are being called. Here's my code:

override func viewDidLoad() {
      super.viewDidLoad()
      locationManager.delegate = self //locationManager is being initialized at the top of class file and is not nil
       locationManager.requestAlwaysAuthorization()
       locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
       locationManager.startUpdatingLocation() //This line executes from viewDidLoad, but not from delegate function
}


    func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) { //This is never called
            if CLLocationManager.authorizationStatus() != .authorizedWhenInUse {
                locationManager.requestWhenInUseAuthorization()
            }else{
                locationManager.startUpdatingLocation()
            }
        }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { //This is never called
        if updatingLocation == true { 
     //if location is updating, go next step
 }
}

I have enabled Privacy - Location Always Usage Description and Privacy - Location When In Use Usage Description in my Info.plist. To test my code, I've also tried to call requestLocation, which should also launch didUpdateLocations, but it didn't. Maybe I'm missing something, but it seems nothing has been changed about CLLocationManager handling for iOS10.

UPD: More code

class HomeViewController: BaseViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var updatingLocation = true
//other stuff
}
1
Did you move or simulate move > 1Km? - shallowThought
Just tried to simulate static location and movement, and the data is being presented, however, I have no clue of why this no longer works without simulation. - Mion Kovalski
Maybe edit the question with your new information. The question seems to be "CLLocationManager does not report the initial location under iOS10" - shallowThought
Edited question - Mion Kovalski

1 Answers

0
votes

Take all locationManager delegate functions out of viewDidLoad():

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.delegate = self //locationManager is being initialized at the top of class file and is not nil
    locationManager.requestAlwaysAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
    locationManager.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) { 
    ...
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    ...
}

Also implement:

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Failed: \(error.localizedDescription)")
}

to see errors you might run into.