KishanSadhwani
KishanSadhwani

Reputation: 158

How to receive callback in ios from javascript?

I am trying to call native function in swift from wkwebview. This is what I have done so far:

Swift Part

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    contentController.add(self, name: "backHomePage")

    config.userContentController = contentController
    self.webView = WKWebView(frame: self.containerView.bounds, configuration: config)

    webView.navigationDelegate = self

    self.containerView.addSubview(self.webView)
    if let url = URL(string: assessmentLink) {
        webView.load(URLRequest(url: url))
    }
}

And

extension WebFormVC: WKScriptMessageHandler {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        print("javascript sending \(message.name), body: \(message.body)")
    }
}

Javascript

function backHomePage(message) {
    window.webkit.messageHandlers.backHomePage.postMessage(message);
}

where message can be any string for example: "success"

I am not currently receiving call back in userContentController didReceive method

UPDATE

I also tried sending data as key value for example window.webkit.messageHandlers.backHomePage.postMessage({"message" :"Javascript to swift!"}); , and it still didn't work.

Upvotes: 2

Views: 9251

Answers (2)

KishanSadhwani
KishanSadhwani

Reputation: 158

Okay so the issue in my case was the call to window.webkit.messageHandlers.backHomePage.postMessage(message); was inside the .js file which was loaded externally and I dont really know why it was not being called.

I solved this issue by injecting an overload of the javascript function, keeping the body same as it is in that external .js file, and injected it at the end of the Document.

Thanks to @Andy for suggesting the idea of injecting userScript in the document. This information might come handy if anyone else faces same issue but is not the answer to the question "How to receive callback in ios from javascript?" so @Andy's answer is accepted since it precisely answers the question.

Upvotes: 2

Andrew
Andrew

Reputation: 673

I've tried to reproduce your case and everything seems to work as expected

import UIKit
import WebKit

class ViewController: UIViewController, WKScriptMessageHandler {
  let content = """
  <!DOCTYPE html><html><body>
  <button onclick="onClick()">Click me</button>
  <script>
  function onClick() {
    window.webkit.messageHandlers.backHomePage.postMessage("success");
  }
  </script>
  </body></html>
  """

  override func viewDidLoad() {
    super.viewDidLoad()

    let config = WKWebViewConfiguration()
    config.userContentController = WKUserContentController()
    config.userContentController.add(self, name: "backHomePage")

    let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 200, height: 200), configuration: config)

    view.addSubview(webView)

    webView.loadHTMLString(content, baseURL: nil)
  }

  func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
    print(message.body)
  }
}

Upvotes: 2

Related Questions