在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:Lision/WKWebViewJavascriptBridge开源软件地址:https://github.com/Lision/WKWebViewJavascriptBridge开源编程语言:Swift 85.2%开源软件介绍:
What Can WKWebViewJavascriptBridge Do?You can write hybrid modules in just a few lines of code by using WKWebViewJavascriptBridge without the need to be concerned with the underlying messaging implementation. Why Only Support WKWebView?Advantages of WKWebViewIt is well known that WKWebView loads web pages faster and more efficiently than UIWebView, and also doesn't have as much memory overhead for you. Under the current timeline, most iOS apps only support iOS 9.0+. UIWebView Cross-Domain Access VulnerabilityThe reason for the iOS platform cross-domain access vulnerability is due to UIWebView turning on the WebKitAllowUniversalAccessFromFileURLs and WebKitAllowFileAccessFromFileURLs options. WKWebView default allowFileAccessFromFileURLs and allowUniversalAccessFromFileURLs option is false. Features
Usage1. Instantiate WKWebViewJavascriptBridge with a WKWebView:bridge = WKWebViewJavascriptBridge(webView: webView) 2. Register a Handler in Native, and Call a JS Handler:bridge.register(handlerName: "testiOSCallback") { (parameters, callback) in
print("testiOSCallback called: \(String(describing: parameters))")
callback?("Response from testiOSCallback")
}
bridge.call(handlerName: "testJavascriptHandler", data: ["foo": "before ready"], callback: nil) 3. Copy and Paste setupWKWebViewJavascriptBridge into Your JS:function setupWKWebViewJavascriptBridge(callback) {
if (window.WKWebViewJavascriptBridge) { return callback(WKWebViewJavascriptBridge); }
if (window.WKWVJBCallbacks) { return window.WKWVJBCallbacks.push(callback); }
window.WKWVJBCallbacks = [callback];
window.webkit.messageHandlers.iOS_Native_InjectJavascript.postMessage(null)
} 4. Finally, Call setupWKWebViewJavascriptBridge and then Use The Bridge to Register Handlers and Call Native Handlers:setupWKWebViewJavascriptBridge(function(bridge) {
/* Initialize your app here */
bridge.registerHandler('testJavascriptHandler', function(data, responseCallback) {
console.log('iOS called testJavascriptHandler with', data)
responseCallback({ 'Javascript Says':'Right back atcha!' })
})
bridge.callHandler('testiOSCallback', {'foo': 'bar'}, function(response) {
console.log('JS got response', response)
})
}) InstallationCocoapods
Carthage
ManuallyEither clone the repo and manually add the Files in WKWebViewJavascriptBridge. RequirementsThis framework requires Contact
LicenseWKWebViewJavascriptBridge is provided under the MIT license. See LICENSE file for details. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论