Skip to content

Commit eedfffc

Browse files
authoredMar 31, 2021
Merge pull request #23 from AdaSupport/SUP-377-error-callback
SUP-377: Error Callback with Example
2 parents 2b02d7c + 4988403 commit eedfffc

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed
 

‎AdaEmbedFramework.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |spec|
22

33
spec.name = "AdaEmbedFramework"
4-
spec.version = "1.3.4"
4+
spec.version = "1.3.5"
55
spec.summary = "Embed the Ada Support SDK in your app."
66
spec.description = "Use the Ada Support SDK to inject the Ada support experience into your app. Visit https://ada.support to learn more."
77
spec.homepage = "https://github.com/AdaSupport/ios-sdk"

‎EmbedFramework/AdaWebHost.swift

+31-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ import SafariServices
1212

1313
public class AdaWebHost: NSObject {
1414

15+
public enum AdaWebHostError: Error {
16+
case WebViewFailedToLoad
17+
case WebViewTimeout
18+
}
19+
20+
private var hasError = false
1521
public var handle = ""
1622
public var cluster = ""
1723
public var language = ""
1824
public var styles = ""
1925
public var greeting = ""
26+
public var webViewTimeout = 30.0
2027

2128
/// Metafields can be passed in during init; use `setMetaFields()`
2229
/// to send values in at runtime
@@ -25,6 +32,8 @@ public class AdaWebHost: NSObject {
2532
public var openWebLinksInSafari = false
2633
public var appScheme = ""
2734

35+
36+
public var webViewLoadingErrorCallback: ((Error) -> Void)? = nil
2837
public var zdChatterAuthCallback: (((@escaping (_ token: String) -> Void)) -> Void)?
2938
public var eventCallbacks: [String: (_ event: [String: Any]) -> Void]?
3039

@@ -65,7 +74,9 @@ public class AdaWebHost: NSObject {
6574
openWebLinksInSafari: Bool = false,
6675
appScheme: String = "",
6776
zdChatterAuthCallback: (((@escaping (_ token: String) -> Void)) -> Void)? = nil,
68-
eventCallbacks: [String: (_ event: [String: Any]) -> Void]? = nil
77+
webViewLoadingErrorCallback: ((Error) -> Void)? = nil,
78+
eventCallbacks: [String: (_ event: [String: Any]) -> Void]? = nil,
79+
webViewTimeout: Double = 30.0
6980
) {
7081
self.handle = handle
7182
self.cluster = cluster
@@ -76,7 +87,10 @@ public class AdaWebHost: NSObject {
7687
self.openWebLinksInSafari = openWebLinksInSafari
7788
self.appScheme = appScheme
7889
self.zdChatterAuthCallback = zdChatterAuthCallback
90+
self.webViewLoadingErrorCallback = webViewLoadingErrorCallback
7991
self.eventCallbacks = eventCallbacks
92+
self.webViewTimeout = webViewTimeout
93+
self.hasError = false
8094

8195
self.reachability = Reachability()!
8296
super.init()
@@ -201,17 +215,30 @@ extension AdaWebHost {
201215
webView.uiDelegate = self
202216

203217
guard let remoteURL = URL(string: "https://\(handle).\(clusterString)ada.support/mobile-sdk-webview/") else { return }
204-
let webRequest = URLRequest(url: remoteURL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 30)
218+
let webRequest = URLRequest(url: remoteURL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: webViewTimeout)
205219
webView.load(webRequest)
206-
207-
// Bind handlers for JS messages
220+
208221
userContentController.add(self, name: "embedReady")
209222
userContentController.add(self, name: "eventCallbackHandler")
210223
userContentController.add(self, name: "zdChatterAuthCallbackHandler")
224+
225+
DispatchQueue.main.asyncAfter(deadline: .now() + webViewTimeout) {
226+
if(!self.hasError && webView.isLoading){
227+
webView.stopLoading();
228+
self.webViewLoadingErrorCallback?(AdaWebHostError.WebViewTimeout)
229+
}
230+
}
231+
232+
211233
}
212234
}
213235

214236
extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
237+
public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
238+
/// Whena reset method is built - we will need to set this back to false
239+
self.hasError = true
240+
self.webViewLoadingErrorCallback?(AdaWebHostError.WebViewFailedToLoad)
241+
}
215242
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
216243
let httpSchemes = ["http", "https"]
217244

‎EmbedFramework/AdaWebHostViewController.swift

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import UIKit
1010
import WebKit
1111

1212
class AdaWebHostViewController: UIViewController {
13-
1413
static func createWebController(with webView: WKWebView) -> AdaWebHostViewController {
1514
let bundle = Bundle(for: AdaWebHostViewController.self)
1615
let storyboard = UIStoryboard(name: "AdaWebHostViewController", bundle: bundle)

‎ExampleApp/ViewController.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ import UIKit
1010
import AdaEmbedFramework
1111

1212
class ViewController: UIViewController {
13-
14-
lazy var adaFramework = AdaWebHost(handle: "nic", appScheme: "adaexampleapp")
13+
lazy var adaFramework = AdaWebHost(handle: "nic", appScheme: "adaexampleapp", webViewLoadingErrorCallback: LoadingErrorCallback, webViewTimeout: 30.0)
1514

1615
@IBOutlet var firstNameField: UITextField!
1716
@IBOutlet var lastNameField: UITextField!
1817
@IBOutlet var emailField: UITextField!
18+
19+
20+
//Example error callback
21+
func LoadingErrorCallback(error: Error){
22+
// Handle any error logic here
23+
view.window?.rootViewController?.dismiss(animated: true, completion: nil)
24+
navigationController?.popToRootViewController(animated: true);
25+
print(error)
26+
}
1927

2028
@IBAction func submitForm(_ sender: UIButton) {
2129
guard

0 commit comments

Comments
 (0)