gpt4 book ai didi

ios - 如果用户未批准位置信息,则每次打开 View 时如何请求访问位置信息的权限

转载 作者:行者123 更新时间:2023-11-28 09:21:39 25 4
gpt4 key购买 nike

我试图打开一个需要访问用户位置的视图控制器。目前我正在运行下面的代码。但是,这只会在用户第一次打开视图时请求其权限。之后,如果用户拒绝,应用程序就会崩溃。
我在viewDidLoad()的开头运行这个函数,然后运行

centerMapOnLocation(CLLocationManager().location!, mapView: mapView)

这一行代码将崩溃并显示错误消息:
线程1:致命错误:在展开可选值时意外找到nil
这是什么原因?
func askForPermission() {
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()

// For use in foreground
self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() == true {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
else {
let alertController = UIAlertController(title: "Location Service Error", message: "Go to settings to approve Location services", preferredStyle: .alert)

// Create OK button
let OKAction = UIAlertAction(title: "Ok", style: .default) { (action:UIAlertAction!) in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)

}
alertController.addAction(OKAction)

// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
self.dismiss(animated: true, completion: nil)
}
alertController.addAction(cancelAction)

// Present Dialog message
self.present(alertController, animated: true, completion:nil)
}
}

我还尝试运行以下函数,但在同一位置遇到崩溃。
func checkAuthorizationStatus() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
break
case .denied:
let alertController = UIAlertController(title: "Location Service Error", message: "Go to settings to approve Location services", preferredStyle: .alert)

// Create OK button
let OKAction = UIAlertAction(title: "Ok", style: .default) { (action:UIAlertAction!) in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)

}
alertController.addAction(OKAction)

// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
self.dismiss(animated: true, completion: nil)
}
alertController.addAction(cancelAction)

// Present Dialog message
self.present(alertController, animated: true, completion:nil)
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
break
case .restricted:
let alertController = UIAlertController(title: "Location Service Error", message: "Go to settings to approve Location services", preferredStyle: .alert)

// Create OK button
let OKAction = UIAlertAction(title: "Ok", style: .default) { (action:UIAlertAction!) in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)

}
alertController.addAction(OKAction)

// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
self.dismiss(animated: true, completion: nil)
}
alertController.addAction(cancelAction)

// Present Dialog message
self.present(alertController, animated: true, completion:nil)
break
case .authorizedAlways: break
}
}

我该怎么解决?

最佳答案

你可以通过给某个特定的功能分配任何特定的按钮动作或某个活动,
`onClick(视图c){
//在这里触发你的代码
函数askForPermission(){….}
}`
另一方面,一个经典的Try-catch语句将很好地防止应用程序崩溃,或者您可以为这个特定事件使用不同的线程。

关于ios - 如果用户未批准位置信息,则每次打开 View 时如何请求访问位置信息的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58043311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com