David David - 4 months ago 16
Swift Question

Show simple alert in iOS 7

I have the following code in my iOS app created with Xcode and written in Swift:

@IBAction func openSettings(sender: AnyObject) {
if #available(iOS 8.0, *) {
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
} else {
// Fallback on earlier versions
}
}


Since iOS 7 doesn't support to link in the settings app I want to show an alert (Go to settings app manually…) instead.

But I don't know how to do this. Can anybody help me please?

Answer

Use UIAlertView to display the message and title. Set delegate to self and inherit UIAlertViewDelegate if you want to handle the button pressed.

@IBAction func openSettings(sender: AnyObject) {
    if #available(iOS 8.0, *) {
        UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
    } else {
        UIAlertView(title: "Alert", message: "Go to settings", delegate: nil, cancelButtonTitle: "OK").show()
    }
}
Comments