【问题标题】:CLLocationManager does not report the initial location under iOS10 (delegate functions not being called)iOS10下CLLocationManager不上报初始位置(委托函数没有被调用)
【发布时间】:2017-05-16 18:04:06
【问题描述】:

我有一个应用程序可以根据用户的位置显示一些数据。它在 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
}

【问题讨论】:

  • 你是移动还是模拟移动>1Km?
  • 刚刚尝试模拟静态位置和运动,并且正在呈现数据,但是,我不知道为什么没有模拟就不再有效。
  • 也许用您的新信息编辑问题。问题似乎是“CLLocationManager 不报告iOS10下的初始位置”
  • 已编辑问题

标签: swift swift3 cllocationmanager ios10


【解决方案1】:

viewDidLoad()中取出所有locationManager委托函数:

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

查看您可能遇到的错误。

【讨论】:

  • 抱歉,打错字了 - 在实际代码中它们已经出来了,但它仍然无法正常工作
  • 没有打印错误?另外,显示你在哪里实例化 CLManager。
  • 不幸的是,它甚至从未因错误而失败,并且没有打印任何内容。
猜你喜欢
  • 2013-12-03
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
  • 2015-03-18
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多