For Swift 3 & Swift 4 :
Since UIAlertView is deprecated, there is the good way for display Alert on Swift 3
let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert) let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in //Do whatever you wantswant here }) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) Deprecated :
This is the swift version inspired by the checked response :
Display AlertView :
let alert = UIAlertView(title: "No network connection", message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") alert.delegate = self alert.show() Add the delegate to your view controller :
class AgendaViewController: UIViewController, UIAlertViewDelegate When user click on button, this code will be executed :
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { }