{"id":164030,"date":"2020-10-06T22:03:42","date_gmt":"2020-10-06T14:03:42","guid":{"rendered":"http:\/\/4563.org\/?p=164030"},"modified":"2020-10-06T22:03:42","modified_gmt":"2020-10-06T14:03:42","slug":"jsbridge-%e5%ae%9e%e7%8e%b0%e5%8e%9f%e7%90%86%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=164030","title":{"rendered":"JSBridge \u5b9e\u73b0\u539f\u7406\u89e3\u6790"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  JSBridge \u5b9e\u73b0\u539f\u7406\u89e3\u6790               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mcuking <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<blockquote>\n<p>\u6587\u7ae0\u9996\u53d1\u4e8e\u6211\u7684\u535a\u5ba2 https:\/\/github.com\/mcuking\/blog\/issues\/39<\/p>\n<\/blockquote>\n<blockquote>\n<p>\u76f8\u5173\u4ee3\u7801\u8bf7\u67e5\u9605 https:\/\/github.com\/mcuking\/JSBridge<\/p>\n<\/blockquote>\n<p>JSBridge \u9879\u76ee\u4ee5 js \u4e0e android \u901a\u4fe1\u4e3a\u4f8b\uff0c\u8bb2\u89e3 JSBridge \u5b9e\u73b0\u539f\u7406\uff0c\u4e0b\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u5728 iOS \uff08 UIWebview \u6216 WKWebview \uff09\u5747\u6709\u5bf9\u5e94\u65b9\u6cd5\u3002<\/p>\n<h2>1. native to js<\/h2>\n<p>\u4e24\u79cd native \u8c03\u7528 js \u65b9\u6cd5\uff0c\u6ce8\u610f\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\u9700\u8981\u5728 JS \u5168\u5c40\u4e0a\u4e0b\u6587\u4e0a<\/p>\n<h4>loadUrl<\/h4>\n<h4>evaluateJavascript<\/h4>\n<h3>1.1 loadUrl<\/h3>\n<pre><code>mWebview.loadUrl(\"javascript: func()\"); <\/code><\/pre>\n<h3>1.2 evaluateJavascript<\/h3>\n<pre><code>mWebview.evaluateJavascript(\"javascript: func()\", new ValueCallback&lt;String&gt;() {     @Override     public void onReceiveValue(String value) {         return;     } }); <\/code><\/pre>\n<h4>\u4e0a\u8ff0\u4e24\u79cd native \u8c03\u7528 js \u7684\u65b9\u5f0f\u5bf9\u6bd4\u5982\u4e0b\u8868\uff1a<\/h4>\n<p>| \u65b9\u5f0f | \u4f18\u70b9 | \u7f3a\u70b9 | | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212; | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; | | loadUrl | \u517c\u5bb9\u6027\u597d | 1. \u4f1a\u5237\u65b0\u9875\u9762 2. \u65e0\u6cd5\u83b7\u53d6 js \u65b9\u6cd5\u6267\u884c\u7ed3\u679c | | evaluateJavascript | 1. \u6027\u80fd\u597d 2. \u53ef\u83b7\u53d6 js \u6267\u884c\u540e\u7684\u8fd4\u56de\u503c | \u4ec5\u5728\u5b89\u5353 4.4 \u4ee5\u4e0a\u53ef\u7528 |<\/p>\n<h2>2. js to native<\/h2>\n<p>\u4e09\u79cd js \u8c03\u7528 native \u65b9\u6cd5<\/p>\n<h4>\u62e6\u622a Url Schema \uff08\u5047\u8bf7\u6c42\uff09<\/h4>\n<h4>\u62e6\u622a prompt alert confirm<\/h4>\n<h4>\u6ce8\u5165 JS \u4e0a\u4e0b\u6587<\/h4>\n<h3>2.1 \u62e6\u622a Url Schema<\/h3>\n<p>\u5373\u7531 h5 \u53d1\u51fa\u4e00\u6761\u65b0\u7684\u8df3\u8f6c\u8bf7\u6c42\uff0cnative \u901a\u8fc7\u62e6\u622a URL \u83b7\u53d6 h5 \u4f20\u8fc7\u6765\u7684\u6570\u636e\u3002<\/p>\n<p>\u8df3\u8f6c\u7684\u76ee\u7684\u5730\u662f\u4e00\u4e2a\u975e\u6cd5\u4e0d\u5b58\u5728\u7684 URL \u5730\u5740\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>\"jsbridge:\/\/methodName?{\"data\": arg, \"cbName\": cbName}\" <\/code><\/pre>\n<p>\u5177\u4f53\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>\"jsbridge:\/\/openScan?{\"data\": {\"scanType\": \"qrCode\"}, \"cbName\": \"handleScanResult\"}\" <\/code><\/pre>\n<p>h5 \u548c native \u7ea6\u5b9a\u4e00\u4e2a\u901a\u4fe1\u534f\u8bae\uff0c\u4f8b\u5982 jsbridge, \u540c\u65f6\u7ea6\u5b9a\u8c03\u7528 native \u7684\u65b9\u6cd5\u540d methodName \u4f5c\u4e3a\u57df\u540d\uff0c\u4ee5\u53ca\u540e\u9762\u5e26\u4e0a\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u53c2\u6570 arg\uff0c\u548c\u63a5\u6536\u8be5\u65b9\u6cd5\u6267\u884c\u7ed3\u679c\u7684 js \u65b9\u6cd5\u540d cbName \u3002<\/p>\n<p>\u5177\u4f53\u53ef\u4ee5\u5728 js \u7aef\u5c01\u88c5\u76f8\u5173\u65b9\u6cd5\uff0c\u4f9b\u4e1a\u52a1\u7aef\u7edf\u4e00\u8c03\u7528\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>window.callbackId = 0;  function callNative(methodName, arg, cb) {     const args = {       data: arg === undefined ? null : JSON.stringify(arg),     };      if (typeof cb === 'function') {       const cbName = 'CALLBACK' + window.callbackId++;       window[cbName] = cb;       args['cbName'] = cbName;     }      const url = 'jsbridge:\/\/' + methodName + '?' + JSON.stringify(args);      ... } <\/code><\/pre>\n<p>\u4ee5\u4e0a\u5c01\u88c5\u4e2d\u8f83\u4e3a\u5de7\u5999\u7684\u662f\u5c06\u7528\u4e8e\u63a5\u6536 native \u6267\u884c\u7ed3\u679c\u7684 js \u56de\u8c03\u65b9\u6cd5 cb \u6302\u8f7d\u5230 window \u4e0a\uff0c\u5e76\u4e3a\u9632\u6b62\u547d\u540d\u51b2\u7a81\uff0c\u901a\u8fc7\u5168\u5c40\u7684 callbackId \u6765\u533a\u5206\uff0c\u7136\u540e\u5c06\u8be5\u56de\u8c03\u51fd\u6570\u5728 window \u4e0a\u7684\u540d\u5b57\u653e\u5728\u53c2\u6570\u4e2d\u4f20\u7ed9 native \u7aef\u3002native \u62ff\u5230 cbName \u540e\uff0c\u6267\u884c\u5b8c\u65b9\u6cd5\u540e\uff0c\u5c06\u6267\u884c\u7ed3\u679c\u901a\u8fc7 native \u8c03\u7528 js \u7684\u65b9\u5f0f\uff08\u4e0a\u9762\u63d0\u5230\u7684\u4e24\u79cd\u65b9\u6cd5\uff09\uff0c\u8c03\u7528 cb \u4f20\u7ed9 h5 \u7aef\uff08\u4f8b\u5982\u5c06\u626b\u63cf\u7ed3\u679c\u4f20\u7ed9 h5 \uff09\u3002<\/p>\n<p>\u81f3\u4e8e\u5982\u4f55\u5728 h5 \u4e2d\u53d1\u8d77\u8bf7\u6c42\uff0c\u53ef\u4ee5\u8bbe\u7f6e window.location.href \u6216\u8005\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 iframe \u8fdb\u884c\u8df3\u8f6c\u3002<\/p>\n<pre><code>function callNative(methodName, arg, cb) {     ...      const url = 'jsbridge:\/\/' + method + '?' + JSON.stringify(args);      \/\/ \u901a\u8fc7 location.href \u8df3\u8f6c     window.location.href = url;      \/\/ \u901a\u8fc7\u521b\u5efa\u65b0\u7684 iframe \u8df3\u8f6c     const iframe = document.createElement('iframe');     iframe.src = url;     iframe.style.width = 0;     iframe.style.height = 0;     document.body.appendChild(iframe);      window.setTimeout(function() {         document.body.removeChild(iframe);     }, 800); } <\/code><\/pre>\n<p>native \u4f1a\u62e6\u622a h5 \u53d1\u51fa\u7684\u8bf7\u6c42\uff0c\u5f53\u68c0\u6d4b\u5230\u534f\u8bae\u4e3a jsbridge \u800c\u975e\u666e\u901a\u7684 http\/https\/file \u7b49\u534f\u8bae\u65f6\uff0c\u4f1a\u62e6\u622a\u8be5\u8bf7\u6c42\uff0c\u89e3\u6790\u51fa URL \u4e2d\u7684 methodName \u3001arg \u3001cbName\uff0c\u6267\u884c\u8be5\u65b9\u6cd5\u5e76\u8c03\u7528 js \u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>\u4e0b\u9762\u4ee5\u5b89\u5353\u4e3a\u4f8b\uff0c\u901a\u8fc7\u8986\u76d6 WebViewClient \u7c7b\u7684 shouldOverrideUrlLoading \u65b9\u6cd5\u8fdb\u884c\u62e6\u622a\uff0candroid \u7aef\u5177\u4f53\u5c01\u88c5\u4f1a\u5728\u4e0b\u9762\u5355\u72ec\u7684\u677f\u5757\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<pre><code>import android.util.Log; import android.webkit.WebView; import android.webkit.WebViewClient;  public class JSBridgeViewClient extends WebViewClient {     @Override     public boolean shouldOverrideUrlLoading(WebView view, String url) {         JSBridge.call(view, url);         return true;     } } <\/code><\/pre>\n<h4>\u62e6\u622a URL Schema \u7684\u95ee\u9898<\/h4>\n<ul>\n<li>\u8fde\u7eed\u53d1\u9001\u65f6\u6d88\u606f\u4e22\u5931<\/li>\n<\/ul>\n<p>\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>window.location.href = \"jsbridge:\/\/callNativeNslog?{\"data\": \"111\", \"cbName\": \"\"}\"; window.location.href = \"jsbridge:\/\/callNativeNslog?{\"data\": \"222\", \"cbName\": \"\"}\"; <\/code><\/pre>\n<p>js \u6b64\u65f6\u7684\u8bc9\u6c42\u662f\u5728\u540c\u4e00\u4e2a\u8fd0\u884c\u903b\u8f91\u5185\uff0c\u5feb\u901f\u7684\u8fde\u7eed\u53d1\u9001\u51fa 2 \u4e2a\u901a\u4fe1\u8bf7\u6c42\uff0c\u7528\u5ba2\u6237\u7aef\u672c\u8eab IDE \u7684 log\uff0c\u6309\u987a\u5e8f\u6253\u5370 111\uff0c222\uff0c\u90a3\u4e48\u5b9e\u9645\u7ed3\u679c\u662f 222 \u7684\u901a\u4fe1\u6d88\u606f\u6839\u672c\u6536\u4e0d\u5230\uff0c\u76f4\u63a5\u4f1a\u88ab\u7cfb\u7edf\u629b\u5f03\u4e22\u6389\u3002<\/p>\n<p>\u539f\u56e0\uff1a\u56e0\u4e3a h5 \u7684\u8bf7\u6c42\u5f52\u6839\u7ed3\u5e95\u662f\u4e00\u79cd\u6a21\u62df\u8df3\u8f6c\uff0c\u8df3\u8f6c\u8fd9\u4ef6\u4e8b\u60c5\u4e0a webview \u4f1a\u6709\u9650\u5236\uff0c\u5f53 h5 \u8fde\u7eed\u53d1\u9001\u591a\u6761\u8df3\u8f6c\u7684\u65f6\u5019\uff0cwebview \u4f1a\u76f4\u63a5\u8fc7\u6ee4\u6389\u540e\u53d1\u7684\u8df3\u8f6c\u8bf7\u6c42\uff0c\u56e0\u6b64\u7b2c\u4e8c\u4e2a\u6d88\u606f\u6839\u672c\u6536\u4e0d\u5230\uff0c\u60f3\u8981\u6536\u5230\u600e\u4e48\u529e\uff1f js \u91cc\u5c06\u7b2c\u4e8c\u6761\u6d88\u606f\u5ef6\u65f6\u4e00\u4e0b\u3002<\/p>\n<pre><code>\/\/\u53d1\u7b2c\u4e00\u6761\u6d88\u606f location.href = \"jsbridge:\/\/callNativeNslog?{\"data\": \"111\", \"cbName\": \"\"}\";  \/\/\u5ef6\u65f6\u53d1\u9001\u7b2c\u4e8c\u6761\u6d88\u606f setTimeout(500,function(){     location.href = \"jsbridge:\/\/callNativeNslog?{\"data\": \"222\", \"cbName\": \"\"}\"; }); <\/code><\/pre>\n<p>\u4f46\u8fd9\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u6b64\u65f6\u662f\u5426\u6709\u5176\u4ed6\u5730\u65b9\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u8fdb\u884c\u8bf7\u6c42\uff0c\u4e3a\u7cfb\u7edf\u89e3\u51b3\u6b64\u95ee\u9898\uff0cjs \u7aef\u53ef\u4ee5\u5c01\u88c5\u4e00\u5c42\u961f\u5217\uff0c\u6240\u6709 js \u4ee3\u7801\u8c03\u7528\u6d88\u606f\u90fd\u5148\u8fdb\u5165\u961f\u5217\u5e76\u4e0d\u7acb\u523b\u53d1\u9001\uff0c\u7136\u540e h5 \u4f1a\u5468\u671f\u6027\u6bd4\u5982 500 \u6beb\u79d2\uff0c\u6e05\u7a7a\u4e00\u6b21\u961f\u5217\uff0c\u4fdd\u8bc1\u5728\u5f88\u5feb\u7684\u65f6\u95f4\u5185\u7edd\u5bf9\u4e0d\u4f1a\u8fde\u7eed\u53d1 2 \u6b21\u8bf7\u6c42\u901a\u4fe1\u3002<\/p>\n<ul>\n<li>URL \u957f\u5ea6\u9650\u5236<\/li>\n<\/ul>\n<p>\u5982\u679c\u9700\u8981\u4f20\u8f93\u7684\u6570\u636e\u8f83\u957f\uff0c\u4f8b\u5982\u65b9\u6cd5\u53c2\u6570\u5f88\u591a\u65f6\uff0c\u7531\u4e8e URL \u957f\u5ea6\u9650\u5236\uff0c\u4ecd\u4ee5\u4e22\u5931\u90e8\u5206\u6570\u636e\u3002<\/p>\n<h3>2.2 \u62e6\u622a prompt alert confirm<\/h3>\n<p>\u5373\u7531 h5 \u53d1\u8d77 alert confirm prompt\uff0cnative \u901a\u8fc7\u62e6\u622a prompt \u7b49\u83b7\u53d6 h5 \u4f20\u8fc7\u6765\u7684\u6570\u636e\u3002<\/p>\n<p>\u56e0\u4e3a alert confirm \u6bd4\u8f83\u5e38\u7528\uff0c\u6240\u4ee5\u4e00\u822c\u901a\u8fc7 prompt \u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<p>\u7ea6\u5b9a\u7684\u4f20\u8f93\u6570\u636e\u7684\u7ec4\u5408\u65b9\u5f0f\u4ee5\u53ca js \u7aef\u5c01\u88c5\u65b9\u6cd5\u7684\u53ef\u4ee5\u7c7b\u4f3c\u4e0a\u9762\u7684 \u62e6\u622a URL Schema \u63d0\u5230\u7684\u65b9\u5f0f\u3002<\/p>\n<pre><code>function callNative(methodName, arg, cb) {     ...      const url = 'jsbridge:\/\/' + method + '?' + JSON.stringify(args);      prompt(url); } <\/code><\/pre>\n<p>native \u4f1a\u62e6\u622a h5 \u53d1\u51fa\u7684 prompt\uff0c\u5f53\u68c0\u6d4b\u5230\u534f\u8bae\u4e3a jsbridge \u800c\u975e\u666e\u901a\u7684 http\/https\/file \u7b49\u534f\u8bae\u65f6\uff0c\u4f1a\u62e6\u622a\u8be5\u8bf7\u6c42\uff0c\u89e3\u6790\u51fa URL \u4e2d\u7684 methodName \u3001arg \u3001cbName\uff0c\u6267\u884c\u8be5\u65b9\u6cd5\u5e76\u8c03\u7528 js \u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>\u4e0b\u9762\u4ee5\u5b89\u5353\u4e3a\u4f8b\uff0c\u901a\u8fc7\u8986\u76d6 WebChromeClient \u7c7b\u7684 onJsPrompt \u65b9\u6cd5\u8fdb\u884c\u62e6\u622a\uff0candroid \u7aef\u5177\u4f53\u5c01\u88c5\u4f1a\u5728\u4e0b\u9762\u5355\u72ec\u7684\u677f\u5757\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<pre><code>import android.webkit.JsPromptResult; import android.webkit.WebChromeClient; import android.webkit.WebView;  public class JSBridgeChromeClient extends WebChromeClient {     @Override     public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {         result.confirm(JSBridge.call(view, message));         return true;     } } <\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u6ca1\u6709\u592a\u5927\u7f3a\u70b9\uff0c\u4e5f\u4e0d\u5b58\u5728\u8fde\u7eed\u53d1\u9001\u65f6\u4fe1\u606f\u4e22\u5931\u3002\u4e0d\u8fc7 iOS \u7684 UIWebView \u4e0d\u652f\u6301\u8be5\u65b9\u5f0f\uff08 WKWebView \u652f\u6301\uff09\u3002<\/p>\n<h3>2.3 \u6ce8\u5165 JS \u4e0a\u4e0b\u6587<\/h3>\n<p>\u5373\u7531 native \u5c06\u5b9e\u4f8b\u5bf9\u8c61\u901a\u8fc7 webview \u63d0\u4f9b\u7684\u65b9\u6cd5\u6ce8\u5165\u5230 js \u5168\u5c40\u4e0a\u4e0b\u6587\uff0cjs \u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 native \u7684\u5b9e\u4f8b\u65b9\u6cd5\u6765\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<p>\u5177\u4f53\u6709\u5b89\u5353 webview \u7684 addJavascriptInterface\uff0ciOS UIWebview \u7684 JSContext\uff0ciOS WKWebview \u7684 scriptMessageHandler \u3002<\/p>\n<p>\u4e0b\u9762\u4ee5\u5b89\u5353 webview \u7684 addJavascriptInterface \u4e3a\u4f8b\u8fdb\u884c\u8bb2\u89e3\u3002<\/p>\n<p>\u9996\u5148 native \u7aef\u6ce8\u5165\u5b9e\u4f8b\u5bf9\u8c61\u5230 js \u5168\u5c40\u4e0a\u4e0b\u6587\uff0c\u4ee3\u7801\u5927\u81f4\u5982\u4e0b\uff0c\u5177\u4f53\u5c01\u88c5\u4f1a\u5728\u4e0b\u9762\u7684\u5355\u72ec\u677f\u5757\u8fdb\u884c\u8bb2\u89e3\uff1a<\/p>\n<pre><code>public class MainActivity extends AppCompatActivity {      private WebView mWebView;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          mWebView = (WebView) findViewById(R.id.mWebView);          ...          \/\/ \u5c06 NativeMethods \u7c7b\u4e0b\u9762\u7684\u63d0\u4f9b\u7ed9 js \u7684\u65b9\u6cd5\u8f6c\u6362\u6210 hashMap         JSBridge.register(\"JSBridge\", NativeMethods.class);          \/\/ \u5c06 JSBridge \u7684\u5b9e\u4f8b\u5bf9\u8c61\u6ce8\u5165\u5230 js \u5168\u5c40\u4e0a\u4e0b\u6587\u4e2d\uff0c\u540d\u5b57\u4e3a _jsbridge\uff0c\u8be5\u5b9e\u4f8b\u5bf9\u8c61\u4e0b\u6709 call \u65b9\u6cd5         mWebView.addJavascriptInterface(new JSBridge(mWebView), \"_jsbridge\");     } }  public class NativeMethods {     \/\/ \u7528\u6765\u4f9b js \u8c03\u7528\u7684\u65b9\u6cd5     public static void methodName(WebView view, JSONObject arg, CallBack callBack) {     } }  public class JSBridge {     private WebView mWebView;      public JSBridge(WebView webView) {         this.mWebView = webView;     }       private  static Map&lt;String, HashMap&lt;String, Method&gt;&gt; exposeMethods = new HashMap&lt;&gt;();      \/\/ \u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06\u4f20\u5165\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u7c7b\u4e0b\u9762\u7528\u4e8e\u63d0\u4f9b\u7ed9 javacript \u7684\u63a5\u53e3\u8f6c\u6210 Map\uff0c\u540d\u5b57\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570     public static void register(String exposeName, Class&lt;?&gt; classz) {         ...     }      \/\/ \u5b9e\u4f8b\u65b9\u6cd5\uff0c\u7528\u4e8e\u63d0\u4f9b\u7ed9 js \u7edf\u4e00\u8c03\u7528\u7684\u65b9\u6cd5     @JavascriptInterface     public String call(String methodName, String args) {         ...     } } <\/code><\/pre>\n<p>\u7136\u540e h5 \u7aef\u53ef\u4ee5\u5728 js \u8c03\u7528 window._jsbridge \u5b9e\u4f8b\u4e0b\u9762\u7684 call \u65b9\u6cd5\uff0c\u4f20\u5165\u7684\u6570\u636e\u7ec4\u5408\u65b9\u5f0f\u53ef\u4ee5\u7c7b\u4f3c\u4e0a\u9762\u4e24\u79cd\u65b9\u5f0f\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>window.callbackId = 0;  function callNative(method, arg, cb) {   let args = {     data: arg === undefined ? null : JSON.stringify(arg)   };    if (typeof cb === 'function') {     const cbName = 'CALLBACK' + window.callbackId++;     window[cbName] = cb;     args['cbName'] = cbName;   }    if (window._jsbridge) {     window._jsbridge.call(method, JSON.stringify(args));   } } <\/code><\/pre>\n<h4>\u6ce8\u5165 JS \u4e0a\u4e0b\u6587\u7684\u95ee\u9898<\/h4>\n<p>\u4ee5\u5b89\u5353 webview \u7684 addJavascriptInterface \u4e3a\u4f8b\uff0c\u5728\u5b89\u5353 4.2 \u7248\u672c\u4e4b\u524d\uff0cjs \u53ef\u4ee5\u5229\u7528 java \u7684\u53cd\u5c04 Reflection API\uff0c\u53d6\u5f97\u6784\u9020\u8be5\u5b9e\u4f8b\u5bf9\u8c61\u7684\u7c7b\u7684\u5167\u90e8\u4fe1\u606f\uff0c\u5e76\u80fd\u76f4\u63a5\u64cd\u4f5c\u8be5\u5bf9\u8c61\u7684\u5185\u90e8\u5c5e\u6027\u53ca\u65b9\u6cd5\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u9020\u6210\u5b89\u5168\u9690\u60a3\uff0c\u4f8b\u5982\u5982\u679c\u52a0\u8f7d\u4e86\u5916\u90e8\u7f51\u9875\uff0c\u8be5\u7f51\u9875\u7684\u6076\u610f js \u811a\u672c\u53ef\u4ee5\u83b7\u53d6\u624b\u673a\u7684\u5b58\u50a8\u5361\u4e0a\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u5728\u5b89\u5353 4.2 \u7248\u672c\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u63d0\u4f9b\u7ed9 js \u8c03\u7528\u7684 java \u65b9\u6cd5\u524d\u52a0\u88c5\u9970\u5668 @JavascriptInterface\uff0c\u6765\u8868\u660e\u4ec5\u8be5\u65b9\u6cd5\u53ef\u4ee5\u88ab js \u8c03\u7528\u3002<\/p>\n<h4>\u4e0a\u8ff0\u4e09\u79cd js \u8c03\u7528 native \u7684\u65b9\u5f0f\u5bf9\u6bd4\u5982\u4e0b\u8868\uff1a<\/h4>\n<p>| \u65b9\u5f0f | \u4f18\u70b9 | \u7f3a\u70b9 | | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212; | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- | | \u62e6\u622a Url Schema \uff08\u5047\u8bf7\u6c42\uff09 | \u65e0\u5b89\u5168\u6f0f\u6d1e | 1. \u8fde\u7eed\u53d1\u9001\u65f6\u6d88\u606f\u4e22\u5931 2. Url \u957f\u5ea6\u9650\u5236\uff0c\u4f20\u8f93\u6570\u636e\u5927\u5c0f\u53d7\u9650 | | \u62e6\u622a prompt alert confirm | \u65e0\u5b89\u5168\u6f0f\u6d1e | iOS \u7684 UIWebView \u4e0d\u652f\u6301\u8be5\u65b9\u5f0f | | \u6ce8\u5165 JS \u4e0a\u4e0b\u6587 | \u5b98\u65b9\u63d0\u4f9b\uff0c\u65b9\u4fbf\u7b80\u6377 | \u5728\u5b89\u5353 4.2 \u4ee5\u4e0b\u6709\u5b89\u5168\u6f0f\u6d1e |<\/p>\n<h2>3. \u5b89\u5353\u7aef java \u7684\u5c01\u88c5<\/h2>\n<p>native \u4e0e h5 \u4ea4\u4e92\u90e8\u5206\u7684\u4ee3\u7801\u5728\u4e0a\u9762\u5df2\u7ecf\u63d0\u5230\u4e86\uff0c\u8fd9\u91cc\u4e3b\u8981\u662f\u8bb2\u8ff0 native \u7aef\u5982\u4f55\u5c01\u88c5\u66b4\u9732\u7ed9 h5 \u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u9996\u5148\u5355\u72ec\u5c01\u88c5\u4e00\u4e2a\u7c7b NativeMethods\uff0c\u5c06\u4f9b h5 \u8c03\u7528\u7684\u65b9\u6cd5\u4ee5\u516c\u6709\u4e14\u9759\u6001\u65b9\u6cd5\u7684\u5f62\u5f0f\u5199\u5165\u3002\u5982\u4e0b\uff1a<\/p>\n<pre><code>public class NativeMethods {     public static void showToast(WebView view, JSONObject arg, CallBack callBack) {         ...     } } <\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u8003\u8651\u5982\u4f55\u5728 NativeMethods \u548c h5 \u4e4b\u524d\u5efa\u7acb\u4e00\u4e2a\u6865\u6881\uff0cJSBridge \u7c7b\u56e0\u8fd0\u800c\u751f\u3002 JSBridge \u7c7b\u4e0b\u4e3b\u8981\u6709\u4e24\u4e2a\u9759\u6001\u65b9\u6cd5 register \u548c call \u3002\u5176\u4e2d register \u65b9\u6cd5\u662f\u7528\u6765\u5c06\u4f9b h5 \u8c03\u7528\u7684\u65b9\u6cd5\u8f6c\u5316\u6210 Map \u5f62\u5f0f\uff0c\u4ee5\u4fbf\u67e5\u8be2\u3002\u800c call \u65b9\u6cd5\u4e3b\u8981\u662f\u7528\u63a5\u6536 h5 \u7aef\u7684\u8c03\u7528\uff0c\u5206\u89e3 h5 \u7aef\u4f20\u6765\u7684\u53c2\u6570\uff0c\u67e5\u627e\u5e76\u8c03\u7528 Map \u4e2d\u7684\u5bf9\u5e94\u7684 Native \u65b9\u6cd5\u3002<\/p>\n<h4>JSBridge \u7c7b\u7684\u9759\u6001\u65b9\u6cd5 register<\/h4>\n<p>\u9996\u5148\u5728 JSBridge \u7c7b\u4e0b\u58f0\u660e\u4e00\u4e2a\u9759\u6001\u5c5e\u6027 exposeMethods\uff0c\u6570\u636e\u7c7b\u578b\u4e3a HashMap \u3002\u7136\u540e\u58f0\u660e\u9759\u6001\u65b9\u6cd5 register\uff0c\u53c2\u6570\u6709\u5b57\u7b26\u4e32 exposeName \u548c\u7c7b classz\uff0c\u5c06 exposeName \u548c classz \u7684\u6240\u6709\u9759\u6001\u65b9\u6cd5 \u7ec4\u5408\u6210\u4e00\u4e2a map\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>{     jsbridge: {         showToast: ...         openScan: ...     } } <\/code><\/pre>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>private  static Map&lt;String, HashMap&lt;String, Method&gt;&gt; exposeMethods = new HashMap&lt;&gt;();  public static void register(String exposeName, Class&lt;?&gt; classz) {     if (!exposeMethods.containsKey(exposeName)) {         exposeMethods.put(exposeName, getAllMethod(classz));     } } <\/code><\/pre>\n<p>\u7531\u4e0a\u53ef\u77e5\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a getAllMethod \u65b9\u6cd5\u7528\u6765\u5c06\u7c7b\u91cc\u7684\u65b9\u6cd5\u8f6c\u5316\u4e3a HashMap \u6570\u636e\u683c\u5f0f\u3002\u5728\u8be5\u65b9\u6cd5\u91cc\u540c\u6837\u58f0\u660e\u4e00\u4e2a HashMap\uff0c\u5e76\u5c06\u6ee1\u8db3\u6761\u4ef6\u7684\u65b9\u6cd5\u8f6c\u5316\u6210 Map\uff0ckey \u4e3a\u65b9\u6cd5\u540d\uff0cvalue \u4e3a\u65b9\u6cd5\u3002<\/p>\n<p>\u5176\u4e2d\u6761\u4ef6\u4e3a \u516c\u6709 public \u9759\u6001 static \u65b9\u6cd5\u4e14\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a Webview \u7c7b\u7684\u5b9e\u4f8b\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a JSONObject \u7c7b\u7684\u5b9e\u4f8b\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3a CallBack \u7c7b\u7684\u5b9e\u4f8b\u3002 (CallBack \u662f\u81ea\u5b9a\u4e49\u7684\u7c7b\uff0c\u540e\u9762\u4f1a\u8bb2\u5230) \u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>private static HashMap&lt;String, Method&gt; getAllMethod(Class injectedCls) {     HashMap&lt;String, Method&gt; methodHashMap = new HashMap&lt;&gt;();      Method[] methods = injectedCls.getDeclaredMethods();      for (Method method: methods) {         if(method.getModifiers()!=(Modifier.PUBLIC | Modifier.STATIC) || method.getName()==null) {             continue;         }         Class[] parameters = method.getParameterTypes();         if (parameters!=null &amp;&amp; parameters.length==3) {             if (parameters[0] == WebView.class &amp;&amp; parameters[1] == JSONObject.class &amp;&amp; parameters[2] == CallBack.class) {                 methodHashMap.put(method.getName(), method);             }         }     }      return methodHashMap; } <\/code><\/pre>\n<h4>JSBridge \u7c7b\u7684\u9759\u6001\u65b9\u6cd5 call<\/h4>\n<p>\u7531\u4e8e\u6ce8\u5165 JS \u4e0a\u4e0b\u6587\u548c\u4e24\u5916\u4e24\u79cd\uff0ch5 \u7aef\u4f20\u8fc7\u6765\u7684\u53c2\u6570\u5f62\u5f0f\u4e0d\u540c\uff0c\u6240\u4ee5\u5904\u7406\u53c2\u6570\u7684\u65b9\u5f0f\u7565\u6709\u4e0d\u540c\u3002 \u4e0b\u9762\u4ee5\u62e6\u622a Prompt \u7684\u65b9\u5f0f\u4e3a\u4f8b\u8fdb\u884c\u8bb2\u89e3\uff0c\u5728\u8be5\u65b9\u5f0f\u4e2d call \u63a5\u6536\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a webView\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f arg\uff0c\u5373 h5 \u7aef\u4f20\u8fc7\u6765\u7684\u53c2\u6570\u3002\u8fd8\u8bb0\u5f97\u62e6\u622a Prompt \u65b9\u5f0f\u65f6 native \u7aef\u548c h5 \u7aef\u7ea6\u5b9a\u7684\u4f20\u8f93\u6570\u636e\u7684\u65b9\u5f0f\u4e48\uff1f<\/p>\n<pre><code>\"jsbridge:\/\/openScan?{\"data\": {\"scanType\": \"qrCode\"}, \"cbName\":\"handleScanResult\"}\" <\/code><\/pre>\n<p>call \u65b9\u6cd5\u9996\u5148\u4f1a\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4ee5 jsbridge \u5f00\u5934\uff08 native \u7aef\u548c h5 \u7aef\u4e4b\u95f4\u7ea6\u5b9a\u7684\u4f20\u8f93\u6570\u636e\u7684\u534f\u8bae\u540d\uff09\uff0c\u7136\u540e\u8be5\u5b57\u7b26\u4e32\u8f6c\u6210 Uri \u683c\u5f0f\uff0c\u7136\u540e\u83b7\u53d6\u5176\u4e2d\u7684 host \u540d\uff0c\u5373\u65b9\u6cd5\u540d\uff0c\u83b7\u53d6 query\uff0c\u5373\u65b9\u6cd5\u53c2\u6570\u548c js \u56de\u8c03\u51fd\u6570\u540d\u7ec4\u5408\u7684\u5bf9\u8c61\u3002\u6700\u540e\u67e5\u627e exposeMethods \u7684\u6620\u5c04\uff0c\u627e\u5230\u5bf9\u5e94\u7684\u65b9\u6cd5\u5e76\u6267\u884c\u8be5\u65b9\u6cd5\u3002<\/p>\n<pre><code>public static String call(WebView webView, String urlString) {      if (!urlString.equals(\"\") &amp;&amp; urlString!=null &amp;&amp; urlString.startsWith(\"jsbridge\")) {         Uri uri = Uri.parse(urlString);          String methodName = uri.getHost();          try {             JSONObject args = new JSONObject(uri.getQuery());             JSONObject arg = new JSONObject(args.getString(\"data\"));             String cbName = args.getString(\"cbName\");               if (exposeMethods.containsKey(\"JSBridge\")) {                 HashMap&lt;String, Method&gt; methodHashMap = exposeMethods.get(\"JSBridge\");                  if (methodHashMap!=null &amp;&amp; methodHashMap.size()!=0 &amp;&amp; methodHashMap.containsKey(methodName)) {                     Method method = methodHashMap.get(methodName);                      if (method!=null) {                         method.invoke(null, webView, arg, new CallBack(webView, cbName));                     }                 }             }         } catch (Exception e) {             e.printStackTrace();         }      }     return null; } <\/code><\/pre>\n<h4>CallBack \u7c7b<\/h4>\n<p>js \u8c03\u7528 native \u65b9\u6cd5\u6210\u529f\u540e\uff0cnative \u6709\u5fc5\u8981\u8fd4\u56de\u7ed9 js \u4e00\u4e9b\u53cd\u9988\uff0c\u4f8b\u5982\u63a5\u53e3\u662f\u5426\u8c03\u7528\u6210\u529f\uff0c\u6216\u8005 native \u6267\u884c\u540e\u7684\u5f97\u5230\u7684\u6570\u636e\uff08\u4f8b\u5982\u626b\u7801\uff09\u3002\u6240\u4ee5 native \u9700\u8981\u6267\u884c js \u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>\u6267\u884c js \u56de\u8c03\u51fd\u6570\u65b9\u5f0f\u672c\u8d28\u662f native \u8c03\u7528 h5 \u7684 js \u65b9\u6cd5\uff0c\u65b9\u5f0f\u4ecd\u65e7\u662f\u4e0a\u9762\u63d0\u5230\u7684\u4e24\u79cd\u65b9\u5f0f evaluateJavascript \u548c loadUrl \u3002\u7b80\u5355\u6765\u8bf4\u53ef\u4ee5\u76f4\u63a5\u5c06 js \u7684\u56de\u8c03\u51fd\u6570\u540d\u4f20\u7ed9\u5bf9\u5e94\u7684 native \u65b9\u6cd5\uff0cnative \u6267\u884c\u901a\u8fc7 evaluateJavascript \u8c03\u7528\u3002<\/p>\n<p>\u4f46\u4e3a\u4e86\u7edf\u4e00\u5c01\u88c5\u8c03\u7528\u56de\u8c03\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a CallBack \u7c7b\uff0c\u5728\u5176\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a apply \u7684\u9759\u6001\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u76f4\u63a5\u8c03\u7528 js \u56de\u8c03\u3002<\/p>\n<p>\u6ce8\u610f\uff1anative \u6267\u884c js \u65b9\u6cd5\u9700\u8981\u5728\u4e3b\u7ebf\u7a0b\u4e0a\u3002<\/p>\n<pre><code>public class CallBack {     private  String cbName;     private WebView mWebView;      public CallBack(WebView webView, String cbName) {         this.cbName = cbName;         this.mWebView = webView;     }      public void apply(JSONObject jsonObject) {         if (mWebView!=null) {             mWebView.post(() -&gt; {                 mWebView.evaluateJavascript(\"javascript:\" + cbName + \"(\" + jsonObject.toString() + \")\", new ValueCallback&lt;String&gt;() {                     @Override                     public void onReceiveValue(String value) {                         return;                     }                 });             });         }     } } <\/code><\/pre>\n<p>\u5230\u6b64\u4e3a\u6b62 JSBridge \u7684\u5927\u81f4\u539f\u7406\u90fd\u8bb2\u5b8c\u4e86\u3002\u4f46\u529f\u80fd\u4ecd\u53ef\u518d\u52a0\u5b8c\u5584\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>native \u6267\u884c js \u65b9\u6cd5\u65f6\uff0c\u53ef\u63a5\u53d7 js \u65b9\u6cd5\u4e2d\u5f02\u6b65\u8fd4\u56de\u7684\u6570\u636e\uff0c\u6bd4\u5982\u5728 js \u65b9\u6cd5\u4e2d\u8bf7\u6c42\u67d0\u4e2a\u63a5\u53e3\u5728\u8fd4\u56de\u6570\u636e\u3002\u76f4\u63a5\u8c03\u7528 webview \u63d0\u4f9b\u7684 evaluateJavascript\uff0c\u5728\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u7c7b ValueCallback \u7684\u5b9e\u4f8b\u65b9\u6cd5 onReceiveValue \u5e76\u4e0d\u80fd\u63a5\u6536\u5230 js \u5f02\u6b65\u8fd4\u56de\u7684\u6570\u636e\u3002<\/p>\n<p>\u540e\u9762\u6709\u7a7a native \u8c03\u7528 js \u65b9\u5f0f\u4f1a\u7ee7\u7eed\u5b8c\u5584\u7684\uff0c\u6700\u540e\u4ee5\u4e00\u53e5\u53e4\u8bed\u4e92\u52c9\uff1a<\/p>\n<p>\u8def\u6f2b\u6f2b\u5176\u4fee\u8fdc\u516e \u543e\u5c06\u4e0a\u4e0b\u800c\u6c42\u7d22<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3591116\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : littleTomorrow1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8d5e\u4e00\u4e2a\uff01\u603b\u7ed3\u7684\u5f88\u5b8c\u6574\u4e86\uff0c\u4e4b\u524d\u4e00\u76f4\u53ea\u77e5\u9053\u4e00\u70b9\u5927\u6982\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>JSBridge \u5b9e\u73b0\u539f\u7406\u89e3\u6790 \u8cc7&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/164030"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=164030"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/164030\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=164030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=164030"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=164030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}