Dismissing UIAlertController automatically
You can achieve the automatic dismiss of alert view by using dispatch_after of GCD.
Try below code :
let delay = 0.5 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) { () -> Void in
self.dismissPopover()
}
Here, dismissCategoryPopover()
is a custom method that will be called automatically after 0.5 seconds to dismiss the alert view.
I have a custom function updated for Swift 2.1 which uses a UIAlertController to simulate the Toast functionality in Android.
func showToast(withMessage message:String) {
let menu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let cancel = UIAlertAction(title: message, style: .Cancel, handler: nil)
menu.addAction(cancel)
self.presentViewController(menu, animated: true, completion: nil)
let delay = 1.5 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
self.tableView.setEditing(false, animated: true)
dispatch_after(time, dispatch_get_main_queue()) { () -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}
}
This shows a complete, working solution for the OP.
Swift 3:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
alert.dismiss(animated: false, completion: nil)
}