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:
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?
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)
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With