Skip to content

Commit d2c66bf

Browse files
authored
Merge pull request #30 from AdaSupport/SUP-608-sign-in-block-and-web-block-not-working
🦋 Fixed sign-in block and web block functionality
2 parents eedfffc + 7735591 commit d2c66bf

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
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.5"
4+
spec.version = "1.3.6"
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

+43-24
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,15 @@ public class AdaWebHost: NSObject {
203203

204204
extension AdaWebHost {
205205
private func setupWebView() {
206+
let wkPreferences = WKPreferences()
207+
wkPreferences.javaScriptCanOpenWindowsAutomatically = true
208+
wkPreferences.javaScriptEnabled = true
206209
let configuration = WKWebViewConfiguration()
207210
let userContentController = WKUserContentController()
208211
let clusterString = cluster.isEmpty ? "" : "\(cluster)."
209212
configuration.userContentController = userContentController
210213
configuration.mediaTypesRequiringUserActionForPlayback = []
214+
configuration.preferences = wkPreferences
211215
webView = WKWebView(frame: .zero, configuration: configuration)
212216
guard let webView = webView else { return }
213217
webView.scrollView.isScrollEnabled = false
@@ -239,33 +243,48 @@ extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
239243
self.hasError = true
240244
self.webViewLoadingErrorCallback?(AdaWebHostError.WebViewFailedToLoad)
241245
}
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 {
243249
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) {
245285
if navigationAction.navigationType == WKNavigationType.linkActivated {
246286
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)
269288
}
270289
decisionHandler(.cancel)
271290
}

0 commit comments

Comments
 (0)