Surge Pedroza Surge Pedroza - 1 year ago 233
Swift Question

Dismiss UIAlertView after 5 Seconds Swift

I've created a UIAlertView that contains a UIActivityIndicator. Everything works great, but I'd also like the UIAlertView to disappear after 5 seconds.

How can I Dismiss my UIAlertView after 5 seconds?

var alert: UIAlertView = UIAlertView(title: "Loading", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel");

var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37)) as UIActivityIndicatorView =;
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray

alert.setValue(loadingIndicator, forKey: "accessoryView")

Answer Source

You can dismiss your UIAlertView after a 5 second delay programmatically, like so:

    // 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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download