@@ -12,11 +12,18 @@ import SafariServices
12
12
13
13
public class AdaWebHost : NSObject {
14
14
15
+ public enum AdaWebHostError : Error {
16
+ case WebViewFailedToLoad
17
+ case WebViewTimeout
18
+ }
19
+
20
+ private var hasError = false
15
21
public var handle = " "
16
22
public var cluster = " "
17
23
public var language = " "
18
24
public var styles = " "
19
25
public var greeting = " "
26
+ public var webViewTimeout = 30.0
20
27
21
28
/// Metafields can be passed in during init; use `setMetaFields()`
22
29
/// to send values in at runtime
@@ -25,6 +32,8 @@ public class AdaWebHost: NSObject {
25
32
public var openWebLinksInSafari = false
26
33
public var appScheme = " "
27
34
35
+
36
+ public var webViewLoadingErrorCallback : ( ( Error ) -> Void ) ? = nil
28
37
public var zdChatterAuthCallback : ( ( ( @escaping ( _ token: String ) -> Void ) ) -> Void ) ?
29
38
public var eventCallbacks : [ String : ( _ event: [ String : Any ] ) -> Void ] ?
30
39
@@ -65,7 +74,9 @@ public class AdaWebHost: NSObject {
65
74
openWebLinksInSafari: Bool = false ,
66
75
appScheme: String = " " ,
67
76
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
69
80
) {
70
81
self . handle = handle
71
82
self . cluster = cluster
@@ -76,7 +87,10 @@ public class AdaWebHost: NSObject {
76
87
self . openWebLinksInSafari = openWebLinksInSafari
77
88
self . appScheme = appScheme
78
89
self . zdChatterAuthCallback = zdChatterAuthCallback
90
+ self . webViewLoadingErrorCallback = webViewLoadingErrorCallback
79
91
self . eventCallbacks = eventCallbacks
92
+ self . webViewTimeout = webViewTimeout
93
+ self . hasError = false
80
94
81
95
self . reachability = Reachability ( ) !
82
96
super. init ( )
@@ -201,17 +215,30 @@ extension AdaWebHost {
201
215
webView. uiDelegate = self
202
216
203
217
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 )
205
219
webView. load ( webRequest)
206
-
207
- // Bind handlers for JS messages
220
+
208
221
userContentController. add ( self , name: " embedReady " )
209
222
userContentController. add ( self , name: " eventCallbackHandler " )
210
223
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
+
211
233
}
212
234
}
213
235
214
236
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
+ }
215
242
public func webView( _ webView: WKWebView , decidePolicyFor navigationAction: WKNavigationAction , decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Swift . Void ) {
216
243
let httpSchemes = [ " http " , " https " ]
217
244
0 commit comments