How can I prompt the user to turn on location services after user has denied their use

后端 未结 11 2225
死守一世寂寞
死守一世寂寞 2020-11-29 20:06

I have an application with an explicit user interaction that makes use of the user\'s current location. If the user denies access to location services, I would still like su

11条回答
  •  清酒与你
    2020-11-29 20:33

    latest swift version based on answers above.

    func showSettingsAlert(_ from:UIViewController, title:String?, message:String?) {
    
            let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
    
            let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)
    
            let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment: ""), style: .default) { (UIAlertAction) in
    
                guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
                    return
                }
    
                if UIApplication.shared.canOpenURL(settingsUrl) {
                    UIApplication.shared.open(settingsUrl, options: [:], completionHandler: nil)
                }
            }
    
            alertController.addAction(cancelAction)
            alertController.addAction(settingsAction)
            from.present(alertController, animated: true, completion: nil)
        }
    

提交回复
热议问题