赞
踩
I guest you are using webView if you not forget window.webkit . You must define it first , by default there is no object (in window):
window.webkit
Just add it :
let contentController = self.webView.configuration.userContentController
contentController.addScriptMessageHandler(self, name: "callbackHandler")
let config = WKWebViewConfiguration()
config.userContentController = contentController
let webView = WKWebView(frame: CGRect.zero, configuration: config)
// Add callback func
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
// message.name is "callbackHandler"
// message.body is ["programming":"js"]
}
Now you can call in your browser env :
var message = {'programming':'js'};
window.webkit.messageHandlers.callbackHandler.postMessage(message);
Reference :
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。