swift - 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
}
非常感谢任何信息!蒂亚!
解决方案
如果您使用startUpdatingLocation()
它通常需要大约一秒钟才能获得第一次更新。第一次更新通常是旧的(缓存的)位置,但它会很快到达。
使用requestLocation()
可能需要几秒钟的时间,因为它会等到它有一个好的位置。
此方法立即返回。调用它会导致位置管理器获取位置修复(这可能需要几秒钟)并使用结果调用委托的 locationManager(_:didUpdateLocations:) 方法。位置定位是在所需精度属性指示的精度级别上获得的。仅向代表报告一个位置修复,之后停止位置服务。
你的问题是你没有打电话requestWhenInUseAuthorization()
给你的位置经理。您必须获得使用位置的许可。除了调用该函数(将键添加到应用程序的 Info.plist)之外,您还需要做一些其他事情。
推荐阅读
- c++ - 如何使 Git 提交哈希在 C++ 代码中可用而无需重新编译?
- c# - 返回视图(“NameOfTheView”)不起作用
- c++ - copy_if 与保存序列并使用复制
- c# - DataAdapter.Fill() 在加载大十进制值时抛出溢出异常
- maven-3 - 在 Ubunto 64 位上编译 hadoop 2.4.0 时出现 Maven 错误
- json - 在 Angular 6 中按字段重新排序 JSON 数据
- reactjs - 动画 CSSTransition 反应
- javascript - 使用 replaceState 时不会触发 onpopstate 事件
- java - 使用 Spring Boot 2 和 mysql 激活 Spring Data JPA saveAll / save 方法非常慢
- java - Java - 检测鼠标是否在屏幕上的任何位置按下