从其他VC返回时,不会调用CLLocationManager

时间:2016-02-11 02:52:04

标签: objective-c swift

当我从另一个视图控制器返回视图时,不会调用CLLocationManager委托。但是如果视图控制器在没有呈现另一个视图控制器的情况下加载,它的工作完全正常。

例如,每次调用if defaults.objectForKey("firstTimeUser") == nil {并且视图控制器被解除时,CLLocationManager委托都不会被调用。

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()
}

我认为它与LocationManager being called inside a class method rather than an instance method有关。还尝试在main_thread中设置LocationManager:

dispatch_async(dispatch_get_main_queue(), {
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.distanceFilter = 3000
    self.locationManager.delegate = self
    self.locationManager.requestWhenInUseAuthorization()
})

0 个答案:

没有答案