|
1 | | -# WKWebViewJavascriptBridge |
| 1 | + |
2 | 2 |
|
3 | | -🌉 A Bridge for Sending Messages between Swift and JavaScript in WKWebViews. |
| 3 | +[](https://github.com/apple/swift) |
| 4 | +[](https://github.com/Carthage/Carthage) |
| 5 | +[](https://raw.githubusercontent.com/Lision/WKWebViewJavascriptBridge/master/LICENSE) |
| 6 | +[](https://www.apple.com/nl/ios/) |
| 7 | +[](http://cocoadocs.org/docsets/WKWebViewJavascriptBridge) |
| 8 | +[](https://travis-ci.org/Lision/WKWebViewJavascriptBridge) |
| 9 | +[](http://cocoapods.org/pods/WKWebViewJavascriptBridge) |
4 | 10 |
|
5 | | -🚧 This framework is under construction! |
| 11 | +> [中文介绍](https://github.com/Lision/WKWebViewJavascriptBridge/blob/master/README_ZH-CN.md) |
| 12 | +
|
| 13 | +# What Can WKWebViewJavascriptBridge Do? |
| 14 | + |
| 15 | +You can write hybrid moudles in just a few lines of code by use WKWebViewJavascriptBridge without need to be concerned with the underlying messaging implementation. |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +# Why Only Support WKWebView? |
| 20 | + |
| 21 | +### Advantages of WKWebView |
| 22 | + |
| 23 | +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. |
| 24 | + |
| 25 | +Under the current timeline, most iOS apps only support iOS 9.0+. |
| 26 | + |
| 27 | +### UIWebView Cross-Domain Access Vulnerability |
| 28 | + |
| 29 | +The reason for the iOS platform cross-domain access vulnerability is due to UIWebView turning on the WebKitAllowUniversalAccessFromFileURLs and WebKitAllowFileAccessFromFileURLs options. |
| 30 | + |
| 31 | +**WKWebView default allowFileAccessFromFileURLs and allowUniversalAccessFromFileURLs option is false.** |
| 32 | + |
| 33 | +# Features |
| 34 | + |
| 35 | +- **Swift Support:** Swift 3.2 ~ 4 Support. |
| 36 | +- **High Performance:** The messaging performance is higher than intercept requests. |
| 37 | +- **High Speed:** No need to consider alert box safety timeout. |
| 38 | +- **Lightwight:** This framework contains only 3 files. |
| 39 | +- **Non-intrusive:** There is no need to make the webview class inherit from other base class. |
| 40 | + |
| 41 | +# Usage |
| 42 | + |
| 43 | +### 1. Instantiate WKWebViewJavascriptBridge with a WKWebView: |
| 44 | + |
| 45 | +``` swift |
| 46 | +bridge = WKWebViewJavascriptBridge(webView: webView) |
| 47 | +``` |
| 48 | + |
| 49 | +### 2. Register a Handler in Native, and Call a JS Handler: |
| 50 | + |
| 51 | +``` swift |
| 52 | +bridge.register(handlerName: "testiOSCallback") { (paramters, callback) in |
| 53 | + print("testiOSCallback called: \(String(describing: paramters))") |
| 54 | + callback?("Response from testiOSCallback") |
| 55 | +} |
| 56 | + |
| 57 | +bridge.call(handlerName: "testJavascriptHandler", data: ["foo": "before ready"], callback: nil) |
| 58 | +``` |
| 59 | + |
| 60 | +### 3. Copy and Paste setupWKWebViewJavascriptBridge into Your JS: |
| 61 | + |
| 62 | +``` js |
| 63 | +function setupWKWebViewJavascriptBridge(callback) { |
| 64 | + if (window.WKWebViewJavascriptBridge) { return callback(WKWebViewJavascriptBridge); } |
| 65 | + if (window.WKWVJBCallbacks) { return window.WKWVJBCallbacks.push(callback); } |
| 66 | + window.WKWVJBCallbacks = [callback]; |
| 67 | + window.webkit.messageHandlers.iOS_Native_InjectJavascript.postMessage(null) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### 4. Finally, Call setupWKWebViewJavascriptBridge and then Use The Bridge to Register Handlers and Call Native Handlers: |
| 72 | + |
| 73 | +``` js |
| 74 | +setupWKWebViewJavascriptBridge(function(bridge) { |
| 75 | + |
| 76 | + /* Initialize your app here */ |
| 77 | + |
| 78 | + bridge.registerHandler('testJavascriptHandler', function(data, responseCallback) { |
| 79 | + console.log('iOS called testJavascriptHandler with', data) |
| 80 | + responseCallback({ 'Javascript Says':'Right back atcha!' }) |
| 81 | + }) |
| 82 | + |
| 83 | + bridge.callHandler('testiOSCallback', {'foo': 'bar'}, function(response) { |
| 84 | + console.log('JS got response', response) |
| 85 | + }) |
| 86 | +}) |
| 87 | +``` |
| 88 | + |
| 89 | +# Installation |
| 90 | + |
| 91 | +### Cocoapods |
| 92 | + |
| 93 | +1. Add `pod 'WKWebViewJavascriptBridge', '~> 1.0.1'` to your Podfile. |
| 94 | +2. Run `pod install` or `pod update`. |
| 95 | +3. Add `import WKWebViewJavascriptBridge`. |
| 96 | + |
| 97 | +### Carthage |
| 98 | + |
| 99 | +1. Add `github "Lision/WKWebViewJavascriptBridge" ~> 1.0.1` to your Cartfile. |
| 100 | +2. Run `carthage update --platform ios`. |
| 101 | +3. Add the `WKWebViewJavascriptBridge` framework to your project. |
| 102 | + |
| 103 | +### Manually |
| 104 | + |
| 105 | +Either clone the repo and manually add the Files in [WKWebViewJavascriptBridge](https://github.com/Lision/WKWebViewJavascriptBridge/tree/master/WKWebViewJavascriptBridge). |
| 106 | + |
| 107 | +# Requirements |
| 108 | + |
| 109 | +This framework requires `iOS 9.0+` and `Xcode 9.0+`. |
| 110 | + |
| 111 | +# Contact |
| 112 | + |
| 113 | + |
| 114 | +- Sina: [@Lision](https://weibo.com/5071795354/profile) |
| 115 | +- Twitter: [@Lision](https://twitter.com/LisionChat) |
| 116 | + |
| 117 | +# License |
| 118 | + |
| 119 | +[](https://raw.githubusercontent.com/Lision/WKWebViewJavascriptBridge/master/LICENSE) |
| 120 | + |
| 121 | +WKWebViewJavascriptBridge is provided under the MIT license. See LICENSE file for details. |
0 commit comments