HoloWebViewBridge
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Usage
Type 1
1, Inject function
let webView = WKWebView()
webView.holo.inject(function: "window.bridge.log") { (args) in
print(args ?? "")
}
2, Call function in JS
window.bridge.log("hello world")
Type 2
1, Inject plugin
let webView = WKWebView()
webView.holo.inject(plugin: WebViewLogPlugin())
2, Define plugin by WebViewPluginProtocol
class WebViewLogPlugin: WebViewPluginProtocol {
func log(_ msg: Any) {
print(msg)
}
// MARK: - WebViewPluginProtocol
var identifier: String {
return "holo.webView.bridge.log"
}
var javascript: String {
if let path = Bundle(for: WebViewLogPlugin.self).path(forResource: "log", ofType: "js"),
let js = try? String(contentsOfFile: path, encoding: .utf8) {
return js
}
return ""
}
func didReceiveMessage(_ fun: String, args: Any?) {
if fun == "log()" {
self.log(msg ?? "")
}
}
}
3, Define log function in log.js
window.bridge.log = function(msg) {
window.bridge.js_msgSend("holo.webView.bridge.log", "log()", msg)
}
4, Call function in JS
window.bridge.log("hello world")
Installation
HoloWebViewBridge is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'HoloWebViewBridge'
Author
gonghonglou, [email protected]
License
HoloWebViewBridge is available under the MIT license. See the LICENSE file for more info.