CLLocationManager不报告iOS10下的初始位置(未调用委托函数)

时间:2017-01-02 16:15:26

标签: swift swift3 cllocationmanager ios10

我有一个应用程序,根据用户的位置显示一些数据。它在Swift2和iOS9上运行良好。现在没有调用CLLocationManager委托的函数。     这是我的代码:

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

我已启用隐私 - 位置始终使用说明和隐私 - 我在Info.plist中使用时的位置使用说明。为了测试我的代码,我还尝试调用requestLocation,它也应该启动didUpdateLocations,但它没有。也许我错过了一些东西,但似乎没有任何关于iOS10的CLLocationManager处理的改变。

UPD:更多代码

class HomeViewController: BaseViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var updatingLocation = true
//other stuff
}

1 个答案:

答案 0 :(得分:0)

locationManager中取出所有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]) {
    ...
}

同时实施:

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

查看您可能遇到的错误。