我正在创建一个基于位置的应用程序,当用户拒绝位置访问时,会弹出一个警告 View ,要求用户转到设置页面并更新设置以允许位置访问。如果用户允许访问然后按回键,则 View 不会使用允许位置更新的更新设置进行刷新。
是否有一个功能可以在用户从设置页面返回到应用程序后运行,该页面将刷新并开始更新位置。
我感谢任何建议或帮助。谢谢。
用户点击返回应用程序后,页面需要刷新以更新 map 上的位置:
func alertForNoLocationAccess(){
let alert = UIAlertController(title: "Allow Location Access", message: "Cannot add location data and map coordinates to your entries without access. Press settings to update or cancel to deny access.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: { action in
if let url = URL(string:UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
最佳答案
将 ViewController/AppDelegate
注册到 NotificationCenter
以在用户从 Settings
打开应用程序时收到通知。
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
在用户打开应用程序时,再次向 CLLocationManager
请求位置更新。如果用户已经接受了请求,那么 CLLocationManager
将开始更新用户的位置而不会中断。
@objc func willEnterForeground() {
//Register for
registerForLocationUpdates()
}
func registerForLocationUpdates() {
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
关于ios - Swift 4 - 更新隐私设置 CLLocationManager 后刷新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53240978/