Ayoola Solomon Ayoola Solomon - 7 months ago 54
Swift Question

How do i open phone settings when a button is clicked ios

I am trying to implement a feature in an App that shows an alert when internet connection is not available.
The alert has two action OK and Settings, whenever a user clicks on settings i want to take them to the phone settings programmatically.

I am using Swift and Xcode-Beta

Answer

Using UIApplicationOpenSettingsURLString

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);
}
Comments