Fetch the repository succeeded.
SwiftJS是用swift写的,用于演示如何和JavaScript进行交互的Demo.
let conf = WKWebViewConfiguration()
let userScript = WKUserScript(source: "redHeader()", injectionTime: .AtDocumentEnd, forMainFrameOnly: true)
conf.userContentController.addUserScript(userScript)
webView = WKWebView(frame: self.view.frame, configuration: conf)
1.首先添加一个WKScriptMessageHandler代理
class ViewController: UIViewController, WKScriptMessageHandler
2.实现«userContentController»的代理方法
func userContentController(userContentController: WKUserContentController!, didReceiveScriptMessage message: WKScriptMessage!) {
if(message.name == "callbackHandler") {
println("JavaScript is sending a message \(message.body)")
}
}
3.WebView启动对JavaScript的监听事件
contentController.addScriptMessageHandler(
self,
name: "callbackHandler"
)
4.H5中,添加如下JavaScript
webkit.messageHandlers.callbackHandler.postMessage("I Love you");
Sign in for post a comment
Activity
Community
Health
Trend
Influence
:Code submit frequency
:React/respond to issue & PR etc.
:Well-balanced team members and collaboration
:Recent popularity of project
:Star counts, download counts etc.
Comments ( 1 )