Dismissing UIAlertController automatically

iphone, swift, uialertcontroller

Solution

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.

Problem

My application has a `UILongPressGestureRecognizer`, which display an alert with `UIAlertController` when the user touches two elements on the screen. Currently, the user must press the OK button in order to dismiss the alert, however I would like to dismiss the alert after 0.5 seconds automatically, so the user doesn't compromise his interaction with the app. Is there any way to do that?

Original source