1

I am using a UIAlertController to display a user alert that requires a user action. I would like to skip the user action and make the alert disappear after some time (say 10 seconds). What is the best approach to do that?

scytale
  • 12,346
  • 3
  • 32
  • 46
Syed Tariq
  • 2,878
  • 3
  • 27
  • 37
  • possible duplicate of [Dismiss UIAlertView after 5 Seconds Swift](http://stackoverflow.com/questions/27613926/dismiss-uialertview-after-5-seconds-swift) – Dharmesh Kheni May 18 '15 at 11:07
  • The approach is applicable but the solution suggested by Chackle works 'almost' out of the box. – Syed Tariq May 18 '15 at 13:26

2 Answers2

2

Try this after creating your UIAlertViewController

var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(10.0 * Double(NSEC_PER_SEC))) 
dispatch_after(dispatchTime, dispatch_get_main_queue(), { 
    yourAlertViewController.dismissViewControllerAnimated(true, completion: nil)   
})
Chackle
  • 2,249
  • 18
  • 34
1

This will work perfectly fine for you

    //Define & Initialize Alert (Example)
    var alert: UIAlertController = UIAlertController();      

    // Delay the execution (Dismiss AlertController) by 10 seconds
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        alert.dismissViewControllerAnimated(true, completion: nil)
    });
Toye
  • 288
  • 2
  • 13