I\'ve created a UIAlertView that contains a UIActivityIndicator. Everything works great, but I\'d also like the UIAlertView to disappear after 5 seconds.
Ho
You can dismiss your UIAlertView
after a 5 second delay programmatically, like so:
alert.show()
// Delay the dismissal by 5 seconds
let delay = 5.0 * Double(NSEC_PER_SEC)
var time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
alert.dismissWithClickedButtonIndex(-1, animated: true)
})
In iOS 8.0+ UIAlertController
inherits from UIViewController
, so, it is just that, a view controller. So, all the restrictions apply. That's why when there's a possibility that the view gets dismissed by the user, it's not entirely safe to try to dismiss it without proper checks.
In the following snippet there's an example of how this can be achieved.
func showAutoDismissableAlert(
title: String?,
message: String?,
actions: [MyActionWithPayload], //This is just an struct holding the style, name and the action in case of the user selects that option
timeout: DispatchTimeInterval?) {
let alertView = UIAlertController(
title: title,
message: message,
preferredStyle: .alert
)
//map and assign your actions from MyActionWithPayload to alert UIAlertAction
//(..)
//Present your alert
//(Here I'm counting on having the following variables passed as arguments, for a safer way to do this, see https://github.com/agilityvision/FFGlobalAlertController)
alertView.present(viewController, animated: animated, completion: completion)
//If a timeout was set, prepare your code to dismiss the alert if it was not dismissed yet
if let timeout = timeout {
DispatchQueue.main.asyncAfter(
deadline: DispatchTime.now() + timeout,
execute: { [weak alertView] in
if let alertView = alertView, !alertView.isBeingDismissed {
alertView.dismiss(animated: true, completion: nil)
}
}
}
}
@ronatory 's answer in c#
var when = new DispatchTime(DispatchTime.Now,
TimeSpan.FromSeconds(5));
DispatchQueue.MainQueue.DispatchAfter(when, () =>
{
// your code with delay
alertController.DismissModalViewController(true);
});
//Generic Function For Dismissing alert w.r.t Timer
/** showWithTimer */
public func showWithTimer(message : String?, viewController : UIViewController?) {
let version : NSString = UIDevice.current.systemVersion as NSString
if version.doubleValue >= 8 {
alert = UIAlertController(title: "", message: message, preferredStyle:.alert)
viewController?.present(alert ?? UIAlertController(), animated:true, completion:nil)
let when = DispatchTime.now() + 5
DispatchQueue.main.asyncAfter(deadline: when){
self.alert?.dismiss(animated: true, completion: nil)
}
}
}
in swift 2 you can do this. Credit to @Lyndsey Scott
let alertController = UIAlertController(title: "youTitle", message: "YourMessage", preferredStyle: .Alert)
self.presentViewController(alertController, animated: true, completion: nil)
let delay = 5.0 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
alertController.dismissViewControllerAnimated(true, completion: nil)
})
For Swift 3
let alert = UIAlertController(title: “Alert”, message: “Message”,preferredStyle: UIAlertControllerStyle.alert)
self.present(alert, animated: true, completion: nil)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double((Int64)(5.0 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {() -> Void in
alert.dismiss(animated: true, completion: {() -> Void in
})
})