From dd4e5b23d93eb96c1df57deedf7b6c97dbcac95a Mon Sep 17 00:00:00 2001 From: Ignacio Tischelman Date: Wed, 30 Oct 2024 15:33:16 -0300 Subject: [PATCH] Fixing web view delegate proxy --- CHANGELOG.md | 2 +- .../WebView/WKNavigationDelegateProxy.swift | 118 +++--------------- .../Logs/LogSemantics+NetworkCapture.swift | 1 - .../WKNavigationDelegateProxyTests.swift | 25 ++-- 4 files changed, 33 insertions(+), 113 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83ea1b2b..86521e56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ * Improved performance during the startup of the SDK. * Fixes * Fixed compilation errors in WatchOS. - * Fixed visbility of `LogLevel`. + * Fixed visibility of `LogLevel`. ## 6.5.0 *Oct 18th, 2024* diff --git a/Sources/EmbraceCore/Capture/WebView/WKNavigationDelegateProxy.swift b/Sources/EmbraceCore/Capture/WebView/WKNavigationDelegateProxy.swift index 6a56fe8e..9a2a054b 100644 --- a/Sources/EmbraceCore/Capture/WebView/WKNavigationDelegateProxy.swift +++ b/Sources/EmbraceCore/Capture/WebView/WKNavigationDelegateProxy.swift @@ -11,6 +11,24 @@ class WKNavigationDelegateProxy: NSObject { // callback triggered the webview loads an url or errors var callback: ((URL?, Int?) -> Void)? + + override func responds(to aSelector: Selector!) -> Bool { + if super.responds(to: aSelector) { + return true + } else if let originalDelegate = originalDelegate, originalDelegate.responds(to: aSelector) { + return true + } + return false + } + + override func forwardingTarget(for aSelector: Selector!) -> Any? { + if super.responds(to: aSelector) { + return self + } else if let originalDelegate = originalDelegate, originalDelegate.responds(to: aSelector) { + return originalDelegate + } + return nil + } } extension WKNavigationDelegateProxy: WKNavigationDelegate { @@ -55,105 +73,5 @@ extension WKNavigationDelegateProxy: WKNavigationDelegate { // call original originalDelegate?.webView?(webView, didFail: navigation, withError: error) } - - // forwarded methods without capture - func webView( - _ webView: WKWebView, - decidePolicyFor navigationAction: WKNavigationAction, - decisionHandler: @escaping (WKNavigationActionPolicy) -> Void - ) { - originalDelegate?.webView?(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler) - ?? decisionHandler(.allow) - } - - func webView( - _ webView: WKWebView, - decidePolicyFor navigationAction: WKNavigationAction, - preferences: WKWebpagePreferences, - decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void - ) { - originalDelegate?.webView?( - webView, - decidePolicyFor: navigationAction, - preferences: preferences, - decisionHandler: decisionHandler - ) - ?? decisionHandler(.allow, preferences) - } - - func webView( - _ webView: WKWebView, - didStartProvisionalNavigation navigation: WKNavigation! - ) { - originalDelegate?.webView?(webView, didStartProvisionalNavigation: navigation) - } - - func webView( - _ webView: WKWebView, - didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation! - ) { - originalDelegate?.webView?(webView, didReceiveServerRedirectForProvisionalNavigation: navigation) - } - - func webView( - _ webView: WKWebView, - didCommit navigation: WKNavigation! - ) { - originalDelegate?.webView?(webView, didCommit: navigation) - } - - func webView( - _ webView: WKWebView, - didFinish navigation: WKNavigation! - ) { - originalDelegate?.webView?(webView, didFinish: navigation) - } - - func webView( - _ webView: WKWebView, - didReceive challenge: URLAuthenticationChallenge, - completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void - ) { - originalDelegate?.webView?(webView, didReceive: challenge, completionHandler: completionHandler) - ?? completionHandler(.performDefaultHandling, nil) - } - - func webViewWebContentProcessDidTerminate( - _ webView: WKWebView - ) { - originalDelegate?.webViewWebContentProcessDidTerminate?(webView) - } - - @available(iOS 14, *) - func webView( - _ webView: WKWebView, - authenticationChallenge challenge: URLAuthenticationChallenge, - shouldAllowDeprecatedTLS decisionHandler: @escaping (Bool) -> Void - ) { - originalDelegate?.webView?( - webView, - authenticationChallenge: challenge, - shouldAllowDeprecatedTLS: decisionHandler - ) - ?? decisionHandler(true) - } - - @available(iOS 14.5, *) - func webView( - _ webView: WKWebView, - navigationAction: WKNavigationAction, - didBecome download: WKDownload - ) { - originalDelegate?.webView?(webView, navigationAction: navigationAction, didBecome: download) - } - - @available(iOS 14.5, *) - func webView( - _ webView: WKWebView, - navigationResponse: WKNavigationResponse, - didBecome download: WKDownload - ) { - originalDelegate?.webView?(webView, navigationResponse: navigationResponse, didBecome: download) - } } #endif diff --git a/Sources/EmbraceSemantics/Logs/LogSemantics+NetworkCapture.swift b/Sources/EmbraceSemantics/Logs/LogSemantics+NetworkCapture.swift index b0629fe0..11a6cca0 100644 --- a/Sources/EmbraceSemantics/Logs/LogSemantics+NetworkCapture.swift +++ b/Sources/EmbraceSemantics/Logs/LogSemantics+NetworkCapture.swift @@ -6,7 +6,6 @@ import EmbraceCommonInternal extension LogType { public static let networkCapture = LogType(system: "network_capture") - } public extension LogSemantics { diff --git a/Tests/EmbraceCoreTests/Capture/WebView/WKNavigationDelegateProxyTests.swift b/Tests/EmbraceCoreTests/Capture/WebView/WKNavigationDelegateProxyTests.swift index 61482a28..78d8b181 100644 --- a/Tests/EmbraceCoreTests/Capture/WebView/WKNavigationDelegateProxyTests.swift +++ b/Tests/EmbraceCoreTests/Capture/WebView/WKNavigationDelegateProxyTests.swift @@ -32,22 +32,25 @@ class WKNavigationDelegateProxyTests: XCTestCase { proxy.webView(webView, decidePolicyFor: WKNavigationResponse(), decisionHandler: block) proxy.webView(webView, didFailProvisionalNavigation: navigation, withError: error) proxy.webView(webView, didFail: navigation, withError: error) - proxy.webView(webView, decidePolicyFor: WKNavigationAction(), decisionHandler: block1) - proxy.webView( + + let delegate = proxy as WKNavigationDelegate + delegate.webView?(webView, decidePolicyFor: WKNavigationAction(), decisionHandler: block1) + + delegate.webView?( webView, decidePolicyFor: WKNavigationAction(), preferences: WKWebpagePreferences(), decisionHandler: block2 ) - proxy.webView(webView, didStartProvisionalNavigation: navigation) - proxy.webView(webView, didReceiveServerRedirectForProvisionalNavigation: navigation) - proxy.webView(webView, didCommit: navigation) - proxy.webView(webView, didFinish: navigation) - proxy.webView(webView, didReceive: URLAuthenticationChallenge(), completionHandler: block3) - proxy.webViewWebContentProcessDidTerminate(webView) - proxy.webView(webView, authenticationChallenge: URLAuthenticationChallenge(), shouldAllowDeprecatedTLS: block4) - proxy.webView(webView, navigationAction: WKNavigationAction(), didBecome: download) - proxy.webView(webView, navigationResponse: WKNavigationResponse(), didBecome: download) + delegate.webView?(webView, didStartProvisionalNavigation: navigation) + delegate.webView?(webView, didReceiveServerRedirectForProvisionalNavigation: navigation) + delegate.webView?(webView, didCommit: navigation) + delegate.webView?(webView, didFinish: navigation) + delegate.webView?(webView, didReceive: URLAuthenticationChallenge(), completionHandler: block3) + delegate.webViewWebContentProcessDidTerminate?(webView) + delegate.webView?(webView, authenticationChallenge: URLAuthenticationChallenge(), shouldAllowDeprecatedTLS: block4) + delegate.webView?(webView, navigationAction: WKNavigationAction(), didBecome: download) + delegate.webView?(webView, navigationResponse: WKNavigationResponse(), didBecome: download) // then the delegate calls are forwarded XCTAssertEqual(originalDelegate.callCount, 14)