0

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.

2
  • You can research some webkit+javascript example. robkerr.com/… Commented Mar 18, 2019 at 6:38
  • its valid question... why its downvoted? Commented Mar 18, 2019 at 12:26

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.