【发布时间】:2017-03-04 00:55:51
【问题描述】:
我有一个使用用户位置搜索周围的按钮。如果他们尚未授予/拒绝使用其位置的权限,它会请求权限
相关代码:
func updateLocationStatus(){
locationEnabled = CLLocationManager.locationServicesEnabled()
appLocationEnabled = CLLocationManager.authorizationStatus()
}
@IBAction func searchNearby(sender: UIButton) {
updateLocationStatus()
if appLocationEnabled == CLAuthorizationStatus.Denied {
//Show an alert to let them know they've denied location permissions
presentViewController(alert, animated: true, completion: nil)
}
else if appLocationEnabled == CLAuthorizationStatus.NotDetermined ||
appLocationEnabled != CLAuthorizationStatus.AuthorizedWhenInUse &&
appLocationEnabled != CLAuthorizationStatus.AuthorizedAlways{
locationManager.requestWhenInUseAuthorization()
}
else{
activityIndicator.startAnimating()
locationManager.requestLocation()
}
}
正常使用时效果很好。但是,如果您让“允许应用程序使用您的位置 [不允许] [允许]”警报停留大约 5 秒钟,则应用程序会崩溃。在 Xcode 中跳转到导致错误的行是
locationManager.requestWhenInUseAuthorization()
提供的错误是
线程 1:EXC_BAD_ACCESS(代码=2,地址=0xsomeaddress)
我什至不知道从哪里开始解决这个问题,因为错误没有告诉我任何信息,并且功能工作得非常好,除非你什么都不做太久
【问题讨论】:
-
回溯是否提供任何有用的信息?
-
是的,确实如此。原来由于我在没有确定授权的情况下调用这个函数,所以在弹出对话框时会重复调用searchNearby函数,直到发生堆栈溢出。我认为指令会按顺序运行(即代码执行在 requestAuthorization 上停止,直到确定答案)但似乎情况并非如此。感谢您的建议,我从不知道回溯是 xcode 中的一件事。请回复为答案,以便我接受它
标签: ios swift core-location