The iOS WKWebView code cannot bridge communication to the web through 'postMessage'

76 Views Asked by At

When a web view is called, a String value is passed through 'postMessage' on the web side when an action occurs inside the web view.

However, currently my code is not aware of any requests from the web.

It doesn't matter where the error occurs. It just doesn't detect any requests from the web.

web code i use

product.onClick = () => {
      sendToApp(product.id)
   ;


export const sendToApp = (path: string) => {
  try {
    Router.postMessage(path);
  } catch (e) {
    devLog('--------ERROR--------');
  }
};

My WebView Code

import SwiftUI
import UIKit
import WebKit

public struct WebViewStruct: UIViewRepresentable {
    
    // ...
    
    public func makeCoordinator() -> WebCoordinator {
        WebCoordinator(self)
    }

    public func makeUIView(context: Context) -> WKWebView {

        let webView = WKWebView(frame: CGRect.zero, configuration: generateWKWebViewConfiguration())
        webView.navigationDelegate = context.coordinator
        webView.scrollView.delegate = context.coordinator

        let myURL: URL = URL(string: "URL")!
        
        let request = URLRequest(url: myURL)
        webView.load(request)
        
        return webView
    }

    public func updateUIView(_ webView: WKWebView, context: Context) { }
}

extension WebViewStruct {

    func generateWKWebViewConfiguration() -> WKWebViewConfiguration {

        let preferences = WKPreferences()
        preferences.javaScriptCanOpenWindowsAutomatically = false

        let configuration = WKWebViewConfiguration()
        configuration.preferences = preferences

        self.registerBridge(name:
            "Router",
        configuration: configuration)

        return configuration
    }

    private func registerBridge(name: String, configuration: WKWebViewConfiguration) {
        configuration.userContentController.add(self.makeCoordinator(), name: name)
    }
}

WebCoordinator

public class WebCoordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler, UIScrollViewDelegate {

    var parent = try! WebViewStruct()

    init(_ uiWebView: WebViewStruct) {
        self.parent = uiWebView
    }

    public func webView(_ webView: WKWebView,
                 decidePolicyFor navigationAction: WKNavigationAction,
                 decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        return decisionHandler(.allow)
    }

    public func userContentController(_ userContentController: WKUserContentController,
                               didReceive message: WKScriptMessage) {
        if message.name == "Router" {
            self.productClicked(message.body)
            print("messageBody")
        }
    }

    public func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
        scrollView.pinchGestureRecognizer?.isEnabled = false
    }
}

extension WebCoordinator {

    private func productClicked(_ messageBody: Any) {
        print(messageBody) // Not Working
    }
}

This problem seems to be caused by my lack of iOS development experience. Please help me

0

There are 0 best solutions below