Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WKWebviewScriptBridge not supporting all WKNavigationDelegate methods #632

Closed
joshuafrancisrak opened this issue Sep 15, 2022 · 2 comments
Closed

Comments

@joshuafrancisrak
Copy link

https://github.com/adjust/ios_sdk/blob/master/AdjustBridge/WebViewJavascriptBridge/WKWebViewJavascriptBridge.m

We are using didCommit and webviewProcessDidTerminate
But since the bridge does not forward events, we can no longer receive those events if we install the AdjustBridge

Can we request to have the other WKNavigationDelegate funcs to be called and forwarded?

@joshuafrancisrak joshuafrancisrak changed the title WKWebviewScriptBridge not supporting all WKNavigationDelegate WKWebviewScriptBridge not supporting all WKNavigationDelegate methods Sep 15, 2022
@joshuafrancisrak
Copy link
Author

joshuafrancisrak commented Sep 16, 2022

For those having problems with unimplemented navigationDelegate funcs, you can use code below
But basically we are adding method to DispatchTable , and sending the message upwards

`
extension WKWebViewJavascriptBridge {
struct SwizzleStatic {
static var once = true
}

class func swizzle() {
    guard SwizzleStatic.once else {
        return
    }
    SwizzleStatic.once = false
    
    let swizzleAddMethod = { (selector: Selector, originalSelector: Selector) in
        guard let method = class_getInstanceMethod(self, selector),
              let imp = class_getMethodImplementation(self, selector) else {
                  assertionFailure("Could not create method / imp")
                  return
              }
        class_addMethod(self, originalSelector, imp, method_getTypeEncoding(method))
        return
    }
    
    let didCommitToAddSelector = #selector(WKWebViewJavascriptBridge.swizzleAdded_webView(_:didCommit:))
    let didCommitSelector = #selector(WKNavigationDelegate.webView(_:didCommit:))
    swizzleAddMethod(didCommitToAddSelector, didCommitSelector)
    
    let terminateToAddSelector = #selector(WKWebViewJavascriptBridge.swizzleAdded_webViewWebContentProcessDidTerminate(_:))
    let terminateSelector = #selector(WKNavigationDelegate.webViewWebContentProcessDidTerminate(_:))
    swizzleAddMethod(terminateToAddSelector, terminateSelector)
}

@objc func swizzleAdded_webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
    let selector = #selector(WKNavigationDelegate.webView(_:didCommit:))
    let object = self.object(thatCanTrigger: selector)
    object?.perform(selector, with: webView, with: navigation)
}

@objc func swizzleAdded_webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
    let selector = #selector(WKNavigationDelegate.webViewWebContentProcessDidTerminate(_:))
    let object = self.object(thatCanTrigger: selector)
    object?.perform(selector, with: webView)
}

private func object(thatCanTrigger selector: Selector) -> NSObjectProtocol? {
    guard let navigationDelegate = self.navigationDelegate,
          navigationDelegate.responds(to: selector) else {
              Logger.default.warning("Navigation delegate cannot be found or does not respond to selector")
              return nil
          }
    
    return navigationDelegate
}

private var navigationDelegate: WKNavigationDelegate? {
    self.value(forKey: "_webViewDelegate") as? WKNavigationDelegate
}

}
`

@uerceg
Copy link
Contributor

uerceg commented Dec 14, 2023

Hi @joshuafrancisrak,

Sorry for the delay in replying and thanks for asking && actually solving this! Glad it worked for you. We're gonna take this ticket and your solution to our backlog to try to add support for this on the level of our web bridge w/o the need for you to do the swizzling.

I'll close this one for now, but will make sure to ping you in here once the functionality has been added to the web bridge. In case you have any questions, always feel free to comment / reopen.

Thank you one more time.

Cheers

@uerceg uerceg closed this as completed Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants