感谢以下Thomas的建议我从原始请求修改了我的代码。我想知道如果我试图做一些不可能的事情,或者如果不行,我该如何做到这一点。在应用程序位置设置更改后重新评估正在运行的应用程序中的CLLocationManager.authorizationStatus
如果用户没有授权位置服务,我通过一个提示与“打开设置”按钮来更改应用程序的位置设置。这工作。但是,从设置返回到应用程序,我想确认是否进行了更改并激活位置服务。这可以做到吗?下面的关闭成功地让用户进入应用程序的设置,用户可以进行更改,用户可以返回,但是当用户按下“打开设置”(在设置被更改之前)时触发关闭。顺便说一句:如果有更好的方法来处理用户批准应用程序当前未授权的位置首选项,我会很感激建议。谢谢!
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? ")
}
我想你是误会是'present'方法。当视图控制器完成呈现时,完成将会触发。我的描述中有点不清楚,但听起来您希望在用户选择了其中一个警报选项后触发此操作,因此此代码应位于UIAlertAction的完成处理程序之一中。 –
谢谢,托马斯。这很有帮助。当用户点击“打开设置”按钮时,我可以移动闭包并执行它。但我想知道我是否在问不可能的事情。我希望在用户通过以下应用程序从应用程序设置返回到应用程序时再次测试授权:UIApplicationOpenSettingsURLString。当用户从更改设置返回时,是否可以以某种方式触发代码并检查我们现在是否具有授权?再次感谢您的建议! – Gallaugher