在应用位置设置更改后重新评估 运行 应用中的 CLLocationManager.authorizationStatus
Reevaluate CLLocationManager.authorizationStatus in running app after app location settings change
感谢下面 Thomas 的建议,我根据原始请求修改了我的代码。我想知道我是否正在尝试做一些不可能的事情,或者如果不是,我该如何完成。
如果用户没有授权位置服务,我会通过带有 "Open Settings" 按钮的警报提示他们更改应用程序的位置设置。这行得通。但是在 return 从设置到应用程序时,我想知道是否进行了更改并激活定位服务。这可以做到吗?下面的闭包成功地让用户进入应用程序的设置,用户可以进行更改,并且用户可以 return,但是当用户按下 "Open Settings" 时闭包会触发,这是在更改设置之前。顺便说一句:如果有更好的方法来处理推动用户批准应用程序当前未经授权的位置首选项,我将不胜感激。谢谢!
locationManager.requestWhenInUseAuthorization() // request authorization
let authStatus = CLLocationManager.authorizationStatus()
switch authStatus {
case .denied:
let alertController = UIAlertController(title: "Background Location Access Disabled", message: "In order to show the location weather forecast, please open this app's settings and set location access to 'While Using'.", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addAction(UIAlertAction(title: "Open Settings", style: .`default`, handler: { action in
if #available(iOS 10.0, *) {
let settingsURL = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(settingsURL, options: [:], completionHandler: {(success) in
print("*** Success closure fires")
let newAuthStatus = CLLocationManager.authorizationStatus()
switch newAuthStatus {
case .authorizedWhenInUse:
self.locationManager.startUpdatingLocation()
default:
print("Not .authorizedWhenInUse")
}
})
} else {
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.shared.openURL(url as URL)
}
}
}))
self.present(alertController, animated: true, completion: nil)
case .authorizedWhenInUse, .authorizedAlways:
locationManager.startUpdatingLocation()
case .restricted :
print("App is restricted, likely via parental controls.")
default:
print("UH!! WHAT OTHER CASES ARE THERE? ")
}
我认为这里的扩展代码完成了我想要的 - 一个简单的获取位置,但它处理所有授权状态情况:
- .notDetermined: requestWhenInUseAuthorization
- .authorized: startUpdatingLocations
- .denied:提示用户使用 UIApplicationOpenSettingsURLString 打开应用程序的隐私/位置设置的警报,以便他们进行更改。在 didUpdateLocations 中获取具有新状态的应用程序的 return,以便在更新设置后捕获用户位置
- .restricted - 显示一条警告,提示用户与家长或系统管理员联系以解除应用限制。
警报还显示 w/error 代码,如果 didFailWithError。
只需为 locationManager 和 currentLocation 设置实例变量
let locationManager = CLLocationManager()
var currentLocation: CLLocation!
并在 viewDidLoad 中设置委托并调用 getLocation()
locationManager.delegate = 自己
getLocation()
希望这是合理的,但欢迎提出更好的方法。希望它对某人有所帮助,因为我一直在努力寻找 Swift 3 和综合性中的内容。再次感谢,托马斯!
扩展名 ViewController:CLLocationManagerDelegate {
func getLocation() {
let status = CLLocationManager.authorizationStatus()
handleLocationAuthorizationStatus(status: status)
}
func handleLocationAuthorizationStatus(status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .authorizedWhenInUse, .authorizedAlways:
locationManager.startUpdatingLocation()
case .denied:
print("I'm sorry - I can't show location. User has not authorized it")
statusDeniedAlert()
case .restricted:
showAlert(title: "Access to Location Services is Restricted", message: "Parental Controls or a system administrator may be limiting your access to location services. Ask them to.")
}
}
func showAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
func statusDeniedAlert() {
let alertController = UIAlertController(title: "Background Location Access Disabled", message: "In order to show the location weather forecast, please open this app's settings and set location access to 'While Using'.", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addAction(UIAlertAction(title: "Open Settings", style: .`default`, handler: { action in
if #available(iOS 10.0, *) {
let settingsURL = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil)
} else {
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.shared.openURL(url as URL)
}
}
}))
self.present(alertController, animated: true, completion: nil)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
handleLocationAuthorizationStatus(status: status)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let currentLocation = locations.last {
print("My coordinates are: \(currentLocation.coordinate.latitude), \(currentLocation.coordinate.longitude)")
locationManager.stopUpdatingLocation()
updateUserInterface()
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
showAlert(title: "Location Access Failure", message: "App could not access locations. Loation services may be unavailable or are turned off. Error code: \(error)")
}
}
感谢下面 Thomas 的建议,我根据原始请求修改了我的代码。我想知道我是否正在尝试做一些不可能的事情,或者如果不是,我该如何完成。
如果用户没有授权位置服务,我会通过带有 "Open Settings" 按钮的警报提示他们更改应用程序的位置设置。这行得通。但是在 return 从设置到应用程序时,我想知道是否进行了更改并激活定位服务。这可以做到吗?下面的闭包成功地让用户进入应用程序的设置,用户可以进行更改,并且用户可以 return,但是当用户按下 "Open Settings" 时闭包会触发,这是在更改设置之前。顺便说一句:如果有更好的方法来处理推动用户批准应用程序当前未经授权的位置首选项,我将不胜感激。谢谢!
locationManager.requestWhenInUseAuthorization() // request authorization
let authStatus = CLLocationManager.authorizationStatus()
switch authStatus {
case .denied:
let alertController = UIAlertController(title: "Background Location Access Disabled", message: "In order to show the location weather forecast, please open this app's settings and set location access to 'While Using'.", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addAction(UIAlertAction(title: "Open Settings", style: .`default`, handler: { action in
if #available(iOS 10.0, *) {
let settingsURL = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(settingsURL, options: [:], completionHandler: {(success) in
print("*** Success closure fires")
let newAuthStatus = CLLocationManager.authorizationStatus()
switch newAuthStatus {
case .authorizedWhenInUse:
self.locationManager.startUpdatingLocation()
default:
print("Not .authorizedWhenInUse")
}
})
} else {
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.shared.openURL(url as URL)
}
}
}))
self.present(alertController, animated: true, completion: nil)
case .authorizedWhenInUse, .authorizedAlways:
locationManager.startUpdatingLocation()
case .restricted :
print("App is restricted, likely via parental controls.")
default:
print("UH!! WHAT OTHER CASES ARE THERE? ")
}
我认为这里的扩展代码完成了我想要的 - 一个简单的获取位置,但它处理所有授权状态情况: - .notDetermined: requestWhenInUseAuthorization - .authorized: startUpdatingLocations - .denied:提示用户使用 UIApplicationOpenSettingsURLString 打开应用程序的隐私/位置设置的警报,以便他们进行更改。在 didUpdateLocations 中获取具有新状态的应用程序的 return,以便在更新设置后捕获用户位置 - .restricted - 显示一条警告,提示用户与家长或系统管理员联系以解除应用限制。
警报还显示 w/error 代码,如果 didFailWithError。
只需为 locationManager 和 currentLocation 设置实例变量
let locationManager = CLLocationManager()
var currentLocation: CLLocation!
并在 viewDidLoad 中设置委托并调用 getLocation()
locationManager.delegate = 自己 getLocation()
希望这是合理的,但欢迎提出更好的方法。希望它对某人有所帮助,因为我一直在努力寻找 Swift 3 和综合性中的内容。再次感谢,托马斯!
扩展名 ViewController:CLLocationManagerDelegate {
func getLocation() {
let status = CLLocationManager.authorizationStatus()
handleLocationAuthorizationStatus(status: status)
}
func handleLocationAuthorizationStatus(status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .authorizedWhenInUse, .authorizedAlways:
locationManager.startUpdatingLocation()
case .denied:
print("I'm sorry - I can't show location. User has not authorized it")
statusDeniedAlert()
case .restricted:
showAlert(title: "Access to Location Services is Restricted", message: "Parental Controls or a system administrator may be limiting your access to location services. Ask them to.")
}
}
func showAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
func statusDeniedAlert() {
let alertController = UIAlertController(title: "Background Location Access Disabled", message: "In order to show the location weather forecast, please open this app's settings and set location access to 'While Using'.", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addAction(UIAlertAction(title: "Open Settings", style: .`default`, handler: { action in
if #available(iOS 10.0, *) {
let settingsURL = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil)
} else {
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.shared.openURL(url as URL)
}
}
}))
self.present(alertController, animated: true, completion: nil)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
handleLocationAuthorizationStatus(status: status)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let currentLocation = locations.last {
print("My coordinates are: \(currentLocation.coordinate.latitude), \(currentLocation.coordinate.longitude)")
locationManager.stopUpdatingLocation()
updateUserInterface()
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
showAlert(title: "Location Access Failure", message: "App could not access locations. Loation services may be unavailable or are turned off. Error code: \(error)")
}
}