From 920eb3abb5db4f85f828e5af7334ddd62348d85f Mon Sep 17 00:00:00 2001 From: hezaichang Date: Wed, 20 May 2020 22:31:47 +0800 Subject: [PATCH] chore: update doc --- README.md | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8d57fa8..fd88f80 100644 --- a/README.md +++ b/README.md @@ -129,8 +129,7 @@ will print 'JS got a message hello' and 'JS responding with' in webview console. ## Notice This lib will inject a WebViewJavascriptBridge Object to window object. -So in your js, before use WebViewJavascriptBridge, you must detect if WebViewJavascriptBridge exist. -If WebViewJavascriptBridge does not exit, you can listen to WebViewJavascriptBridgeReady event, as the blow code shows: +You can listen to `WebViewJavascriptBridgeReady` event to ensure `window.WebViewJavascriptBridge` is exist, as the blow code shows: ```javascript @@ -148,6 +147,38 @@ If WebViewJavascriptBridge does not exit, you can listen to WebViewJavascriptBri ``` +Or put all JsBridge function call into `window.WVJBCallbacks` array if `window.WebViewJavascriptBridge` is undefined, this taks queue will be flushed when `WebViewJavascriptBridgeReady` event triggered. + +Copy and paste setupWebViewJavascriptBridge into your JS: + +```javascript +function setupWebViewJavascriptBridge(callback) { + if (window.WebViewJavascriptBridge) { + return callback(WebViewJavascriptBridge); + } + if (window.WVJBCallbacks) { + return window.WVJBCallbacks.push(callback); + } + window.WVJBCallbacks = [callback]; +} +``` + +Call `setupWebViewJavascriptBridge` and then use the bridge to register handlers or call Java handlers: + +```javascript +setupWebViewJavascriptBridge(function(bridge) { + bridge.registerHandler('JS Echo', function(data, responseCallback) { + console.log("JS Echo called with:", data); + responseCallback(data); + }); + bridge.callHandler('ObjC Echo', {'key':'value'}, function(responseData) { + console.log("JS received response:", responseData); + }); +}); +``` + +It same with https://github.com/marcuswestin/WebViewJavascriptBridge, that would be easier for you to define same behavior in different platform between Android and iOS. Meanwhile, writing concise code. + ## License This project is licensed under the terms of the MIT license.