summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/UIAlertController+Error.swift
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2020-01-07 13:14:20 +0100
committerAndrej Mihajlov <and@mullvad.net>2020-01-08 15:02:46 +0100
commit94e03486f4fffc636c01edb371ed474f236d92e4 (patch)
tree890e06193804dc1f2797a93046825e390f146cc4 /ios/MullvadVPN/UIAlertController+Error.swift
parent29bacffd7d6f42e90bf8a09995cfe16261711635 (diff)
downloadmullvadvpn-94e03486f4fffc636c01edb371ed474f236d92e4.tar.xz
mullvadvpn-94e03486f4fffc636c01edb371ed474f236d92e4.zip
Add extension for UIAlertController to present Errors
Diffstat (limited to 'ios/MullvadVPN/UIAlertController+Error.swift')
-rw-r--r--ios/MullvadVPN/UIAlertController+Error.swift52
1 files changed, 52 insertions, 0 deletions
diff --git a/ios/MullvadVPN/UIAlertController+Error.swift b/ios/MullvadVPN/UIAlertController+Error.swift
new file mode 100644
index 0000000000..b76958b0ef
--- /dev/null
+++ b/ios/MullvadVPN/UIAlertController+Error.swift
@@ -0,0 +1,52 @@
+//
+// UIAlertController+Error.swift
+// MullvadVPN
+//
+// Created by pronebird on 11/12/2019.
+// Copyright © 2019 Amagicom AB. All rights reserved.
+//
+
+import Foundation
+import UIKit
+
+/// An extension for presenting `LocalizedError` subclasses in `UIAlertController`
+extension UIAlertController {
+
+ convenience init<Error>(_ error: Error, preferredStyle: UIAlertController.Style)
+ where Error: LocalizedError
+ {
+ let title = error.errorDescription
+ let message = [error.failureReason, error.recoverySuggestion]
+ .compactMap { $0 }
+ .joined(separator: "\n\n")
+
+ self.init(title: title, message: message, preferredStyle: preferredStyle)
+ }
+
+}
+
+extension UIViewController {
+
+ /// Present an instance of `LocalizedError` using `UIAlertController`
+ /// Note: this method adds a default "OK" action when `configurationBlock` is not given
+ func presentError<Error>(
+ _ error: Error,
+ preferredStyle: UIAlertController.Style,
+ configurationBlock: ((UIAlertController) -> Void)? = nil,
+ completionBlock: (() -> Void)? = nil)
+ where Error: LocalizedError
+ {
+ let alertController = UIAlertController(error, preferredStyle: preferredStyle)
+
+ if let configurationBlock = configurationBlock {
+ configurationBlock(alertController)
+ } else {
+ alertController.addAction(
+ UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .cancel)
+ )
+ }
+
+ self.present(alertController, animated: true, completion: completionBlock)
+ }
+
+}