@@ -203,11 +203,15 @@ public class AdaWebHost: NSObject {
203
203
204
204
extension AdaWebHost {
205
205
private func setupWebView( ) {
206
+ let wkPreferences = WKPreferences ( )
207
+ wkPreferences. javaScriptCanOpenWindowsAutomatically = true
208
+ wkPreferences. javaScriptEnabled = true
206
209
let configuration = WKWebViewConfiguration ( )
207
210
let userContentController = WKUserContentController ( )
208
211
let clusterString = cluster. isEmpty ? " " : " \( cluster) . "
209
212
configuration. userContentController = userContentController
210
213
configuration. mediaTypesRequiringUserActionForPlayback = [ ]
214
+ configuration. preferences = wkPreferences
211
215
webView = WKWebView ( frame: . zero, configuration: configuration)
212
216
guard let webView = webView else { return }
213
217
webView. scrollView. isScrollEnabled = false
@@ -239,33 +243,48 @@ extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
239
243
self . hasError = true
240
244
self . webViewLoadingErrorCallback ? ( AdaWebHostError . WebViewFailedToLoad)
241
245
}
242
- public func webView( _ webView: WKWebView , decidePolicyFor navigationAction: WKNavigationAction , decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Swift . Void ) {
246
+
247
+ // Shared function to handle opening of urls
248
+ public func openUrl( webView: WKWebView , url: URL ) -> Swift . Void {
243
249
let httpSchemes = [ " http " , " https " ]
244
-
250
+ let urlScheme = url. scheme
251
+ // Handle opening universal links within the host App
252
+ // This requires the appScheme argument to work
253
+ if urlScheme == self . appScheme {
254
+ guard let presentingVC = findViewController ( from: webView) else { return }
255
+ presentingVC. dismiss ( animated: true ) {
256
+ let shared = UIApplication . shared
257
+ if shared. canOpenURL ( url) {
258
+ shared. open ( url, options: [ : ] , completionHandler: nil )
259
+ }
260
+ }
261
+ // Only open links in in-app WebView if URL uses HTTP(S) scheme, and the openWebLinksInSafari option is false
262
+ // This is where SUP-43 is likely crashing
263
+ } else if self . openWebLinksInSafari == false && httpSchemes. contains ( urlScheme ?? " " ) {
264
+ let sfVC = SFSafariViewController ( url: url)
265
+ guard let presentingVC = findViewController ( from: webView) else { return }
266
+ presentingVC. present ( sfVC, animated: true , completion: nil )
267
+ } else {
268
+ let shared = UIApplication . shared
269
+ if shared. canOpenURL ( url) {
270
+ shared. open ( url, options: [ : ] , completionHandler: nil )
271
+ }
272
+ }
273
+ }
274
+
275
+ // Used for weblinks and signon (handling window.open js call)
276
+ public func webView( _ webView: WKWebView , createWebViewWith configuration: WKWebViewConfiguration , for navigationAction: WKNavigationAction , windowFeatures: WKWindowFeatures ) -> WKWebView ? {
277
+ if let url = navigationAction. request. url {
278
+ openUrl ( webView: webView, url: url)
279
+ }
280
+ return nil
281
+ }
282
+
283
+ // Used for processing all other navigation
284
+ public func webView( _ webView: WKWebView , decidePolicyFor navigationAction: WKNavigationAction , decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Swift . Void ) {
245
285
if navigationAction. navigationType == WKNavigationType . linkActivated {
246
286
if let url = navigationAction. request. url {
247
- let urlScheme = url. scheme
248
- // Handle opening universal links within the host App
249
- // This requires the appScheme argument to work
250
- if urlScheme == self . appScheme {
251
- guard let presentingVC = findViewController ( from: webView) else { return }
252
- presentingVC. dismiss ( animated: true ) {
253
- let shared = UIApplication . shared
254
- if shared. canOpenURL ( url) {
255
- shared. open ( url, options: [ : ] , completionHandler: nil )
256
- }
257
- }
258
- // Only open links in in-app WebView if URL uses HTTP(S) scheme, and the openWebLinksInSafari option is false
259
- } else if self . openWebLinksInSafari == false && httpSchemes. contains ( urlScheme ?? " " ) {
260
- let sfVC = SFSafariViewController ( url: url)
261
- guard let presentingVC = findViewController ( from: webView) else { return }
262
- presentingVC. present ( sfVC, animated: true , completion: nil )
263
- } else {
264
- let shared = UIApplication . shared
265
- if shared. canOpenURL ( url) {
266
- shared. open ( url, options: [ : ] , completionHandler: nil )
267
- }
268
- }
287
+ openUrl ( webView: webView, url: url)
269
288
}
270
289
decisionHandler ( . cancel)
271
290
}
0 commit comments