rv7284
rv7284

Reputation: 1102

How to pass function to webpage using swift?

I have a situation where I need to capture an event from a button click in a WebPage.

this code is on the webpage on a button click.

okCoolClick = () => {
/*eslint-disable* /
console.log("okay cool ", window.okCool);
if (window.okCool) {
  okCool.performClick();
} else {
  window.open("some-url");
}
};

Now the Android team is done with the problem, with below code.

commonWebView.addJavascriptInterface(object : Any() {
           @JavascriptInterface
           fun performClick() {
               val ownerId = arguments?.getInt(OWNER_ID)
               if (ownerId == -1) {
                   context?.startActivity<RightNavigationActivity>()
                   activity?.finishAffinity()
               } else {
                   context?.startActivity<HomeItemListActivity>(
                           Pair(HomeItemListActivity.INTENT_EXTRA_COMPONENT,
                                   AppConstants.HOME_SUB_LIST_CLUB_OUTLET_LIST),
                           Pair(HomeItemListActivity.INTENT_EXTRA_CLUB_ID, ownerId),
                           Pair(HomeItemListActivity.INDUSTRY_ID, -1)
                   )
                   activity?.finish()
               }
           }
       }, "okCool")

So, Now I'm left with finding the solution in iOS,

How can I achieve this on my iOS project? Any help or node toward the correct direction would be great.

Upvotes: 0

Views: 752

Answers (2)

Sumeet.Jain
Sumeet.Jain

Reputation: 1609

In ViewDidLoad() add this

let config = WKWebViewConfiguration()
let contentController = WKUserContentController()
contentController.add(self, name: "function")
config.userContentController = contentController
webView = WKWebView(frame: .zero, configuration: config)

add two methods from WKNavigationDelegate delegate:

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
    return decisionHandler(.allow)
}

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
{
    if navigationAction.navigationType == WKNavigationType.linkActivated, let url = navigationAction.request.url{
        DeepLink.deepLink(url.absoluteString)
    }
    return decisionHandler(.allow)
}

Finally add the most important method to get callback from WKScriptMessageHandler delegate :

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage){
    let dataFromWeb = message.body //Data Passed from webview
}

Pass data from html like this

var myObj = {"name":"John", "age":30, "car":null};
window.webkit.messageHandlers.function.postMessage(myObj);

"function" name should be same in the above statement and when u add userContentController as a config for your webview.

Upvotes: 3

Ali Adam
Ali Adam

Reputation: 331

You can use this WebViewJavascriptBridge for sending messages between Obj-C/Swift and JavaScript.

Also you can do it without using any Third Party read this.

Upvotes: 0

Related Questions