2

I have a UIAlert that notifies the user that they do not have an internet connection and that they need one in order to use the app. As well as letting them dismiss the alert by tapping the ok action I also want to have a action that when tapped takes the user to the settings app.

func displayAlert(title: String, message: String){

    var formEmpty = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

    formEmpty.addAction((UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in

    })))
Larme
  • 24,190
  • 6
  • 51
  • 81
Tom Fox
  • 897
  • 3
  • 14
  • 34

3 Answers3

3

Use this code . May be help it.

override func viewDidAppear(animated: Bool) {
    var alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .Alert)

    var settingsAction = UIAlertAction(title: "Settings", style: .Default) { (_) -> Void in
        let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
        if let url = settingsUrl {
            UIApplication.sharedApplication().openURL(url)
        }
    }

    var cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
    alertController.addAction(settingsAction)
    alertController.addAction(cancelAction)

    presentViewController(alertController, animated: true, completion: nil);
}

Please note UIApplicationOpenSettingsURLString is only available on iOS8.0 and after so if your app should support iOS7 you'll have to check for availability of the constant (or if using Swift 2.0 use the #availability keyword).

Fantattitude
  • 1,842
  • 2
  • 18
  • 34
Hardik Shekhat
  • 1,680
  • 12
  • 21
0

You can navigate to the setting with this code:

let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(settingsUrl!)

After adding this code in your function your function will look like:

func displayAlert(title: String, message: String){

    var formEmpty = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

    formEmpty.addAction((UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
        //This will call when you press ok in your alertview
        let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
        UIApplication.sharedApplication().openURL(settingsUrl!)
    })))
}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • Ah, I just discovered a slight problem. When you click on the settings button in the alert it takes you to the settings of the app, whereas I want to direct the user to the wifi settings. @Dharmesh – Tom Fox Jul 29 '15 at 07:55
  • @TomFox That is just not possible, no public API gives you access to this. – Fantattitude Jul 29 '15 at 09:19
0

For iOS 10, Swift 3:

    let alert = UIAlertController(title: "Alert!", message: "your message here", preferredStyle: .alert)
    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
    let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
    UIApplication.shared.open(settingsUrl as! URL, options: [:], completionHandler: nil)
    alert.addAction(settingsAction)
    present(alert, animated: true, completion: nil)
Ashiq Sulaiman
  • 631
  • 1
  • 6
  • 14