Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlert in Swift that automatically disappears?

I have the following code:

    /// Creates Alerts on screen for user.
func notifyUser(title: String, message: String) -> Void
{
    let alert = UIAlertController(title: title,
        message: message,
        preferredStyle: UIAlertControllerStyle.Alert)

    let cancelAction = UIAlertAction(title: "OK",
        style: .Cancel, handler: nil)

    alert.addAction(cancelAction)
    UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true,
        completion: nil)
}

Which shows the following Alert:

enter image description here

I would prefer the Alert to appear for maybe 1-2 seconds and auto dismiss without having to click ok or dismiss. Is this possible?

like image 435
Malorrr Avatar asked Mar 13 '23 11:03

Malorrr


2 Answers

Here Is the code for Swift 4 Please Refer...Thank you

  let alert = UIAlertController(title: "Success", message: "Record Updated Successfully", preferredStyle: UIAlertController.Style.alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
        switch action.style{
        case .default:
            print("default")

        case .cancel:
            print("cancel")

        case .destructive:
            print("destructive")

        }}))
    self.present(alert, animated: true, completion: nil)
    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {

        alert.dismiss(animated: true, completion: nil)
    }
like image 29
Fansad PP Avatar answered Mar 18 '23 23:03

Fansad PP


Yes it's completely possible, I think the @Duncan C approach will work very well and it's self explanatory, so I going to explain you in code the @Duncan approach and another approach is using delays with the Grand Central Dispatch(GCD).

First Approach: Using the NSTimer class

// set the UIAlerController property
var alert: UIAlertController!

func notifyUser(title: String, message: String, timeToDissapear: Int) -> Void
{
    alert = UIAlertController(title: title,
        message: message,
        preferredStyle: UIAlertControllerStyle.Alert)

    let cancelAction = UIAlertAction(title: "OK",
        style: .Cancel, handler: nil)

    alert.addAction(cancelAction)
    UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true,
        completion: nil)

    // setting the NSTimer to close the alert after timeToDissapear seconds.
    _ = NSTimer.scheduledTimerWithTimeInterval(Double(timeToDissapear), target: self, selector: Selector("dismissAlert"), userInfo: nil, repeats: false)
}

Second Approach: Using GCD

// set the UIAlerController property
var alert: UIAlertController! 

func notifyUser(title: String, message: String, timeToDissapear: Int) -> Void
{
    alert = UIAlertController(title: title,
        message: message,
        preferredStyle: UIAlertControllerStyle.Alert)

    let cancelAction = UIAlertAction(title: "OK",
        style: .Cancel, handler: nil)

    alert.addAction(cancelAction)
    UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true,
        completion: nil)

    // Delay the dismissal by timeToDissapear seconds
    let delay = Double(timeToDissapear) * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue()) { [weak self] in
        self!.alert.dismissViewControllerAnimated(true, completion: nil)
    }
}

And then you can call it in anywhere you want like in the following way :

self.notifyUser("Hello", message: "World", timeToDissapear: 3)

I hope this help you.

like image 91
Victor Sigler Avatar answered Mar 19 '23 00:03

Victor Sigler