swift - 调用 requestLocation() 时的“线程 1:信号 Sigabrt”
问题描述
尝试使用CLlocationManager().requestLocation()
来获取用户的当前位置。但是应用程序崩溃并显示信号 Sigabrt。我知道这可能是因为未连接的插座,所以我确保它们都已连接但编译器仍然会弹出此警告。顺便说一句,我已经正确实现了 p.list 中的键。
有趣的是,当我更改requestLocation()
为时startUpdatingLocation()
,一切正常。
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestLocation()
}
}
我希望有人能告诉我我做错了什么,或者我应该用 startUpdatingLocation() 替换 requestLocation()。
解决方案
Apple 在文档中声明,如果您希望使用,您必须实现这些委托方法requestLocation()
:
locationManager(_:didUpdateLocations:)
和locationManager(_:didFailWithError:)
。
下面的代码对我有用。
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("LOCATIONS: \(locations)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("ERROR: \(error)")
}
}
推荐阅读
- xamarin - 如何在 CollectionView 中顺利加载大量图像?
- sql - 将模型运行日期添加到查询中
- ios - SWIFT PDFKIT 连字符无法处理合理的文本
- docker - 使用 docker-compose 链接到网络中的容器
- c# - 错误 CS0234 名称空间“MvvmCross”中不存在类型或名称空间名称“Platforms”(您是否缺少程序集引用?)
- python - 熊猫申请上一行
- python - 创建新的 Pandas DataFrame 列,与同一 DataFrame 中其他行的值相等
- mysql - 使用 SQL 为每个产品完成的总销售额
- python - 在列表的 Pandas 数据框列中查找最大值
- ssl - 配置镜像仓库需要什么样的证书?