summaryrefslogtreecommitdiffhomepage
path: root/ios
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
parent29bacffd7d6f42e90bf8a09995cfe16261711635 (diff)
downloadmullvadvpn-94e03486f4fffc636c01edb371ed474f236d92e4.tar.xz
mullvadvpn-94e03486f4fffc636c01edb371ed474f236d92e4.zip
Add extension for UIAlertController to present Errors
Diffstat (limited to 'ios')
-rw-r--r--ios/MullvadVPN.xcodeproj/project.pbxproj4
-rw-r--r--ios/MullvadVPN/UIAlertController+Error.swift52
2 files changed, 56 insertions, 0 deletions
diff --git a/ios/MullvadVPN.xcodeproj/project.pbxproj b/ios/MullvadVPN.xcodeproj/project.pbxproj
index afc0800ad1..ea382d9c6d 100644
--- a/ios/MullvadVPN.xcodeproj/project.pbxproj
+++ b/ios/MullvadVPN.xcodeproj/project.pbxproj
@@ -59,6 +59,7 @@
5894FC492296A8090017471D /* CustomButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5894FC482296A8090017471D /* CustomButton.swift */; };
589AB4F7227B64450039131E /* BasicTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 589AB4F6227B64450039131E /* BasicTableViewCell.swift */; };
58A8BE81239FBE62006B74AC /* IPEndpoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58561C98239A5D1500BD6B5E /* IPEndpoint.swift */; };
+ 58A8BE8323A0F362006B74AC /* UIAlertController+Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58A8BE8223A0F362006B74AC /* UIAlertController+Error.swift */; };
58ADDB3C227B1BD200FAFEA7 /* JsonRpc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58ADDB3B227B1BD200FAFEA7 /* JsonRpc.swift */; };
58ADDB3E227B1CD900FAFEA7 /* MullvadAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58ADDB3D227B1CD900FAFEA7 /* MullvadAPI.swift */; };
58AEEF652344A36000C9BBD5 /* KeychainError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58AEEF642344A36000C9BBD5 /* KeychainError.swift */; };
@@ -182,6 +183,7 @@
5894E725236B2801008A2793 /* SwiftUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftUI.framework; path = System/Library/Frameworks/SwiftUI.framework; sourceTree = SDKROOT; };
5894FC482296A8090017471D /* CustomButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomButton.swift; sourceTree = "<group>"; };
589AB4F6227B64450039131E /* BasicTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasicTableViewCell.swift; sourceTree = "<group>"; };
+ 58A8BE8223A0F362006B74AC /* UIAlertController+Error.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIAlertController+Error.swift"; sourceTree = "<group>"; };
58ADDB3B227B1BD200FAFEA7 /* JsonRpc.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JsonRpc.swift; sourceTree = "<group>"; };
58ADDB3D227B1CD900FAFEA7 /* MullvadAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MullvadAPI.swift; sourceTree = "<group>"; };
58AEEF642344A36000C9BBD5 /* KeychainError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeychainError.swift; sourceTree = "<group>"; };
@@ -348,6 +350,7 @@
58AEEF6A2344A46200C9BBD5 /* TunnelConfigurationManager.swift */,
5845F837236C466400B2D93C /* TunnelControlViewController.swift */,
5835B7CB233B76CB0096D79F /* TunnelManager.swift */,
+ 58A8BE8223A0F362006B74AC /* UIAlertController+Error.swift */,
587CBFE222807F530028DED3 /* UIColor+Helpers.swift */,
58CCA0152242560B004F3011 /* UIColor+Palette.swift */,
581CBCE52296B97300727D7F /* ViewControllerIdentifier.swift */,
@@ -643,6 +646,7 @@
5894FC492296A8090017471D /* CustomButton.swift in Sources */,
58CCA01E2242787B004F3011 /* AccountTextField.swift in Sources */,
587AD7CA2342283900E93A53 /* Account.swift in Sources */,
+ 58A8BE8323A0F362006B74AC /* UIAlertController+Error.swift in Sources */,
587425C12299833500CA2045 /* RootContainerViewController.swift in Sources */,
588AE72F2362001F009F9F2E /* MutuallyExclusive.swift in Sources */,
5888AD89227B18C40051EB06 /* RelayList.swift in Sources */,
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)
+ }
+
+}