Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UIAlertController+Extension
// Extension
extension UIAlertController {
convenience init(with error: Error) {
var title: String!
var message: String!
if let error = error as? LocalizedError, let errorDescription = error.errorDescription {
title = errorDescription
if let recoverySuggestion = error.recoverySuggestion {
message = recoverySuggestion
}
} else {
let nativeError = error as NSError
title = nativeError.localizedDescription
message = nativeError.localizedRecoverySuggestion ?? ""
}
self.init(title: title, message: message, preferredStyle: .alert)
addOKAction()
}
func addOKAction() {
addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .cancel, handler: nil))
}
}
// Implementation
let alert = UIAlertController(with: error)
self.present(alert, animated: true, completion: nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment