Last active
January 22, 2018 16:13
-
-
Save rtking1993/26a4d36a69563f38f5e726949b6edd67 to your computer and use it in GitHub Desktop.
UIAlertController+Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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