SwiftJS是用swift写的,用于演示如何和JavaScript进行交互的Demo.
- JavaScript call Native App
- Native App call JavaScript
- iOS 8.0+ / Mac OS X 10.9+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 7.2+
- 如果您需要帮助,请与我联系hengchengfei@sina.com
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");