简体   繁体   English

如何从 javascript 接收 ios 中的回调?

[英]How to receive callback in ios from javascript?

I am trying to call native function in swift from wkwebview.我正在尝试从 wkwebview 调用 swift 中的原生 function。 This is what I have done so far:这是我到目前为止所做的:

Swift Part Swift 零件

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 Javascript

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

where message can be any string for example: "success"其中 message 可以是任何字符串,例如:“success”

I am not currently receiving call back in userContentController didReceive method我目前没有在userContentController didReceive方法中收到回调

UPDATE更新

I also tried sending data as key value for example window.webkit.messageHandlers.backHomePage.postMessage({"message":"Javascript to swift;"});我还尝试将数据作为键值发送,例如window.webkit.messageHandlers.backHomePage.postMessage({"message":"Javascript to swift;"}); , and it still didn't work. ,它仍然没有工作。

Okay so the issue in my case was the call to window.webkit.messageHandlers.backHomePage.postMessage(message);好的,所以我的问题是调用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.在外部加载的 .js 文件中,我真的不知道为什么它没有被调用。

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.我通过注入 javascript function 的重载解决了这个问题,保持正文与该 external.js 文件中的相同,并在文档末尾注入它。

Thanks to @Andy for suggesting the idea of injecting userScript in the document.感谢@Andy 提出在文档中注入 userScript 的想法。 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?"如果其他人面临同样的问题,此信息可能会派上用场,但不是“如何从 javascript 在 ios 中接收回调?”问题的答案。 so @Andy's answer is accepted since it precisely answers the question.所以@Andy的回答被接受了,因为它准确地回答了这个问题。

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)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM