HoloWebViewBridge
示例
要运行示例项目,请克隆仓库,并首先从Example目录运行 pod install
使用
类型1
1, 注入函数
let webView = WKWebView()
webView.holo.inject(function: "window.bridge.log") { (args) in
print(args ?? "")
}
2, 在JS中调用函数
window.bridge.log("hello world")
类型2
1, 注入插件
let webView = WKWebView()
webView.holo.inject(plugin: WebViewLogPlugin())
2, 通过 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, 在 log.js
中定义日志函数
window.bridge.log = function(msg) {
window.bridge.js_msgSend("holo.webView.bridge.log", "log()", msg)
}
4, 在JS中调用函数
window.bridge.log("hello world")
安装
HoloWebViewBridge 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'HoloWebViewBridge'
作者
gonghonglou, [email protected]
许可证
HoloWebViewBridge 按照 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。