如果应用的位置隐私访问被拒绝怎么办?
大家好,我正在编写一个简单的应用程序,它在使用时使用位置。
设计模式
首先,当您启动应用程序时,它会检查是否已设置权限。
- 如果没有,它会显示一个请求许可的警报。
- 如果是并获得批准,它将继续执行其工作。
- 如果是但被拒绝,它会显示一条警报,要求通过指向设置的按钮授予访问权限。
- 应该可以从设置返回到应用程序。
应用程序发挥作用。
- 如果用户在应用程序仍处于打开状态时更改隐私设置,则应通知该应用程序,并重复第 1 步。
到目前为止的代码
主 Controller
let manager = LocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// ...
if manager.getPermission() == false {
//show alert
showAcessDeniedAlert()
}
manager.onLocationFix = {
//This is a function used for a closure
}
}
}
func showAcessDeniedAlert() {
let alertController = UIAlertController(title: "Location Accees Requested",
message: "The location permission was not authorized. Please enable it in Settings to continue.",
preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (alertAction) in
// THIS IS WHERE THE MAGIC HAPPENS!!!!
if let appSettings = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(appSettings as URL)
}
}
alertController.addAction(settingsAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
位置管理器
import CoreLocation
extension Coordinate {
init(location: CLLocation) {
latitude = location.coordinate.latitude
longitude = location.coordinate.longitude
}
}
final class LocationManager: NSObject, CLLocationManagerDelegate {
let manager = CLLocationManager()
var onLocationFix: ((Coordinate) -> Void)?
override init() {
super.init()
manager.delegate = self
manager.requestLocation()
}
func getPermission() -> Bool {
switch CLLocationManager.authorizationStatus() {
case .authorizedAlways:
return true
case .authorizedWhenInUse:
return true
case .denied:
return false
case .restricted:
return false
case .notDetermined:
manager.requestWhenInUseAuthorization()
return getPermission()
}
}
//MARK: CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
manager.requestLocation()
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else { return }
let coordinate = Coordinate(location: location)
if let onLocationFix = onLocationFix {
onLocationFix(coordinate)
}
}
}
我该怎么做?
如果隐私被拒绝,我如何显示 AlertController?
使用此设置我遇到此错误:
Warning: Attempt to present <UIAlertController: 0x145ae7ee0> on <xxx.xxController: 0x143e04720> whose view is not in the window hierarchy!
.如何编写指向“设置”的设置按钮?
我如何编写代码:“从设置页面,我可以返回应用程序”?
最佳答案
viewDidLoad
在第一次访问self.view
属性后被调用。这意味着在将此self.view
添加到窗口层次结构之后,首先调用viewDidLoad
。将检查代码移动到viewDidAppear(_:)
函数,并确保呈现您的 View Controller 。- 打开设置应用的代码似乎没问题,但不要忘记检查它在哪个系统版本中可用。
- 您的应用在后台状态下无法以某种方式与其他应用进行交互。
关于swift - iOS swift 3 : Location Privacy Denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40150638/