0

CLLocationManager delegate doesn't get called when I return to a view from another view controller. But it works perfectly fine if the view controller loads without presenting another view controller.

For example, everytime if defaults.objectForKey("firstTimeUser") == nil { is called and that view controller is dismissed, CLLocationManager delegate doesn't get called.

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)
    let defaults = NSUserDefaults.standardUserDefaults()
    if defaults.objectForKey("firstTimeUser") == nil {
        if let servicesController = self.storyboard?.instantiateViewControllerWithIdentifier("ServicesController") as? UITableViewController {
            self.navigationController?.presentViewController(servicesController, animated: true, completion: nil)
        }
        defaults.setObject("set", forKey: "firstTimeUser")
        defaults.synchronize()
    } else {
        getLocationData()
        loadNewsData()
    }   
}

func getLocationData() {
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.distanceFilter = 3000
    self.locationManager.delegate = self
    self.locationManager.requestWhenInUseAuthorization()
}

I believe it has something to do with LocationManager being called inside a class method rather than an instance method. Also tried setting LocationManager in the main_thread:

dispatch_async(dispatch_get_main_queue(), {
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.distanceFilter = 3000
    self.locationManager.delegate = self
    self.locationManager.requestWhenInUseAuthorization()
})
Community
  • 1
  • 1
Onichan
  • 4,476
  • 6
  • 31
  • 60
  • When `defaults.objectForKey("firstTimeUser") == nil` is true, you don't call `getLocationData`, and so the delegate is never set. – Michael Feb 11 '16 at 02:56
  • @michael Correct, and the code presents `servicesController`, but when I dismiss `servicesController` and return to the view controller, `getLocationData()` delegates are not called. – Onichan Feb 11 '16 at 02:59
  • So do you know that `viewDidAppear` is rerunning when you dismiss the VC? For example if the VC is displayed as a popover on an iPad, the previous VC's viewDidAppear won't rerun. – Michael Feb 11 '16 at 04:14

0 Answers0