首页 > 解决方案 > CLLocationManager.requestLocation 调用其代表需要多长时间

问题描述

我们收到了一份报告,其中一些用户卡在我们应用程序的某个屏幕上,无法继续访问应用程序的仪表板。

检查有问题的屏幕后,这是一个“加载”屏幕,将在用户成功登录后显示。唯一可能导致用户卡在这里的代码是 CLLocationManager。屏幕将等待 CLLocationManager 调用 didUpdateLocations 或 didFailWithError 然后继续到应用程序的仪表板。

我的猜测是 CLLocationManage 没有调用它的任何代表。因此,用户被困在“加载”屏幕中。

CLLocationManager.requestLocation 调用它的委托函数需要多长时间?如果请求时间过长,CCLocationManager 会调用 didFailWithError 吗?

以下是我们代码的缩短版本:

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    // process the location then forward to dashboard screen
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    // forward to dashboard screen
}

非常感谢任何信息!蒂亚!

标签: swiftcllocationmanager

解决方案


如果您使用startUpdatingLocation()它通常需要大约一秒钟才能获得第一次更新。第一次更新通常是旧的(缓存的)位置,但它会很快到达。

使用requestLocation()可能需要几秒钟的时间,因为它会等到它有一个好的位置。

此方法立即返回。调用它会导致位置管理器获取位置修复(这可能需要几秒钟)并使用结果调用委托的 locationManager(_:didUpdateLocations:) 方法。位置定位是在所需精度属性指示的精度级别上获得的。仅向代表报告一个位置修复,之后停止位置服务。

你的问题是你没有打电话requestWhenInUseAuthorization()给你的位置经理。您必须获得使用位置的许可。除了调用该函数(将键添加到应用程序的 Info.plist)之外,您还需要做一些其他事情。


推荐阅读