Skip to content

Instantly share code, notes, and snippets.

@mohsinbmwm3
Created January 18, 2021 14:46
Show Gist options
  • Save mohsinbmwm3/4bd06dedda42d9cd3a50b2cd8d96c0de to your computer and use it in GitHub Desktop.
Save mohsinbmwm3/4bd06dedda42d9cd3a50b2cd8d96c0de to your computer and use it in GitHub Desktop.
Some useful UIAlertController extension in swift
import Foundation
extension UIAlertController {
static func showSimpleAlert(title: String, message: String, context: UIViewController) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
context.present(alert, animated: true, completion: nil)
}
static func showSimpleAlert(title: String, message: String, okButtonTitle: String, okButtonAction: (@escaping () -> ()), context: UIViewController) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: okButtonTitle, style: .default, handler: { action in
okButtonAction()
}))
context.present(alert, animated: true, completion: nil)
}
static func showAlertWith2Button(title: String, message: String, firstButtonTitle: String, firstButtonAction: (@escaping () -> ()), secondButtonTitle: String, seconButtonAction: (@escaping () -> ()), context: UIViewController) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: firstButtonTitle, style: .default, handler: { action in
firstButtonAction()
}))
alert.addAction(UIAlertAction(title: secondButtonTitle, style: .destructive, handler: { action in
seconButtonAction()
}))
context.present(alert, animated: true, completion: nil)
}
static func showLogoutAlert(context: UIViewController) {
let alert = UIAlertController(title: "Logout", message: "You are logged out of the app successfully.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
context.present(alert, animated: true, completion: nil)
}
static func showInvlaidCredentialAlert(context: UIViewController) {
let alert = UIAlertController(title: "My App", message: "Invalid credentials.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
context.present(alert, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment