【发布时间】:2019-02-02 02:44:11
【问题描述】:
我正在尝试使用位置管理器在应用中检索用户的位置;如 Apple 文档中所述,我创建了以下方法:
func startReceivingLocationChanges() {
let authorizationStatus = CLLocationManager.authorizationStatus()
if authorizationStatus != .authorizedWhenInUse && authorizationStatus != .authorizedAlways {
locationManager.requestWhenInUseAuthorization()
startReceivingLocationChanges()
return
}
if !CLLocationManager.locationServicesEnabled() {
displayError(withTitle: "Location Not Available", withDescription: "Enable Location Services at Settings > Privacy > Location Services", sender: self)
return
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 100.0 // In meters.
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.activityType = .other
locationManager.delegate = self
locationManager.startUpdatingLocation()
}
但是当我启动应用程序时,此崩溃显示错误“线程 1:EXC_BAD_ACCESS (code=2, address=0x16f0a7f60)”靠近行:
locationManager.requestWhenInUseAuthorization()
我指定我在 info.plist 中添加了相对的“隐私 - 始终位置和使用时使用说明”和“隐私 - 使用时位置使用说明”键。
有谁知道是什么导致了这个问题?谢谢。
【问题讨论】:
-
locationManager 初始化了吗?
-
@Andrea 是的,我用“let locationManagaer = CLLocationManager()”将它初始化为一个类变量
标签: swift xcode location swift4 cllocationmanager