How to Listen to JavaScript CallBack from Swift?

I am implementing MPGS webView for payment on WKWebView in my iOS App
the backend developer send me javascript callback on myHandler object..i should receive that handler which carry many callback for success or failure Or cancel (triggered when user click on cancel button in the webview) and in JS it look like this:

 > window.webkit.MyHandler.postMessage('cancel');
    >             window.MyHandler.cancelCallback();

i implement the receiving of that callbacks using:

> override func viewDidLoad() {
>             super.viewDidLoad()
>             let contentController = WKUserContentController()
>             contentController.add(self, name: "MyHandler")
>             let configuration = WKWebViewConfiguration()
>             configuration.userContentController = contentController
>             configuration.preferences = WKPreferences()
>             configuration.preferences.javaScriptEnabled = true
>             let url = URL(string: NetworkManager.mpgsBaseUrl + "/" + (MPGSWebView.url ?? ""))!
>             let urlRequest = URLRequest(url: url)
>             wkWebView = WKWebView(frame: .zero,configuration: configuration)
>             wkWebView.navigationDelegate = self
>             wkWebView.uiDelegate = self
>             wkWebView.load(urlRequest)
>             delegate = self.checkOutViewController
>             view = wkWebView
>         }

the MKwebview is loaded but i am not receiving any events when ii complete or cancel the webview..print statement in the WKScriptMessageHandler delegate never executed:

func userContentController(_ userContentController:

> WKUserContentController, didReceive message: WKScriptMessage) {
>         
>         print(message.name,"fff",message)
>     }

if anyone have an idea of how to recieve that events i will be thankful..best regards

Leave a Comment