• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Lision/WKWebViewJavascriptBridge:

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

Lision/WKWebViewJavascriptBridge

开源软件地址:

https://github.com/Lision/WKWebViewJavascriptBridge

开源编程语言:

Swift 85.2%

开源软件介绍:

language  Carthage compatible  License MIT  Support  CocoaPods  Build Status  CocoaPods

中文介绍

This project is inspired by WebViewJavascriptBridge!

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 WKWebView

It 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 Vulnerability

The 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

  • Swift Support: Swift 3.2 ~ 5 Support.
  • High Performance: The messaging performance is higher than intercept requests.
  • High Speed: No need to consider alert box safety timeout.
  • Lightweight: This framework contains only 3 files.
  • Non-intrusive: There is no need to make the webview class inherit from other base class.

Usage

1. 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)
	})
})

Installation

Cocoapods

  1. Add pod 'WKWebViewJavascriptBridge', '~> 1.2.0' to your Podfile.
  2. Run pod install or pod update.
  3. Add import WKWebViewJavascriptBridge.

Carthage

  1. Add github "Lision/WKWebViewJavascriptBridge" ~> 1.2.0 to your Cartfile.
  2. Run carthage update --platform ios.
  3. Add the WKWebViewJavascriptBridge framework to your project.

Manually

Either clone the repo and manually add the Files in WKWebViewJavascriptBridge.

Requirements

This framework requires iOS 9.0+ and Xcode 9.0+.

Contact

License

WKWebViewJavascriptBridge is provided under the MIT license. See LICENSE file for details.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap