{"id":1144,"date":"2023-01-07T09:56:00","date_gmt":"2023-01-07T01:56:00","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=1144"},"modified":"2022-12-21T09:59:29","modified_gmt":"2022-12-21T01:59:29","slug":"webview-%e4%b8%8e-js-%e4%ba%a4%e4%ba%92%e6%96%b9%e6%a1%88-%e9%80%82%e7%94%a8-android-ios","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=1144","title":{"rendered":"WebView \u4e0e JS \u4ea4\u4e92\u65b9\u6848 &#8211; \u9002\u7528 Android &#038; iOS"},"content":{"rendered":"\n<p><strong>webview \u4e0e JS \u4ea4\u4e92\u5206\u4e3a\u4e24\u79cd\uff1a<\/strong><\/p>\n\n\n\n<ul><li>Android &amp; iOS \u8c03\u7528 JS \u7684\u65b9\u6cd5<\/li><li>JS \u8c03\u7528 Android &amp; iOS \u7684\u65b9\u6cd5<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Android &amp; iOS \u8c03\u7528 JS \u7684\u65b9\u6cd5\uff0c\u4f2a\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<ul><li>Android <em><\/em><code>webView.loadUrl(\"javascript:show('xxx');\");<\/code><\/li><li>iOS <em><\/em><code>NSString *result = [self.webView stringByEvaluatingJavaScriptFromString:@\"showReturn('xxx');\"];<\/code><\/li><\/ul>\n\n\n\n<p>webview \u8c03\u7528 JS \u7684\u65b9\u6cd5\u6bd4\u8f83\u7b80\u5355\uff0c<code>show('xxx')<\/code> \u65b9\u6cd5\u662f JS \u4e2d\u5b9a\u4e49\u7684\u4e00\u4e2a\u65b9\u6cd5\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script language=javascript&gt;\n   function show(str) {\n      alert(str);\n   }\n    \n   function showReturn(str) {\n      return \"result\";\n   }\n&lt;\/script&gt;\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7f3a\u9677\u5f88\u660e\u663e\uff1a<\/p>\n\n\n\n<ol><li>Android \u6ca1\u6cd5\u62ff\u5230\u8fd4\u56de\u503c\uff1b\u4f46\u662f\uff0ciOS\u662f\u53ef\u4ee5\u62ff\u5230\u8fd4\u56de\u503c\u7684\uff0c\u8fd9\u662f\u6700\u91cd\u8981\u7684\u533a\u522b\uff01\uff01\uff01\u53e6\u5916\uff0c\u6211\u4eec\u4e5f\u65e0\u6cd5\u4f20\u9012\u4e00\u4e2a\u56de\u8c03\u63a5\u53e3<code>Callback<\/code>\u7528\u4e8e\u56de\u8c03\uff0c\u4e5f\u5c31\u662f\u8bf4\u6b64\u65b9\u6cd5\u8c03\u7528\u6210\u529f\u4e0e\u5426\uff0c\u662f\u65e0\u6cd5\u77e5\u9053\u7684\u3002<\/li><li><code>show<\/code>\u65b9\u6cd5\u5fc5\u987b\u662f JS \u4e2d\u5b58\u5728\u7684\uff0c\u5373\u4f7f\u4e0d\u5b58\u5728\u4f60\u8c03\u7528\u4e86\u4e5f\u4e0d\u4f1a\u62a5\u9519\uff1b\u53e6\u5916\uff0c\u968f\u7740\u4e1a\u52a1\u7684\u589e\u957f\uff0c\u6211\u4eec\u4e0d\u5f97\u4e0d\u589e\u52a0\u8bb8\u8bb8\u591a\u591a\u7c7b\u4f3c<code>show<\/code>\u7684\u65b9\u6cd5\uff0c\u6765\u5904\u7406\u5176\u4ed6\u4e1a\u52a1\u3002<\/li><\/ol>\n\n\n\n<p><strong>JS \u8c03\u7528 Android &amp; iOS \u7684\u65b9\u6cd5\uff0c\u4f2a\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<ul><li>Android <em><\/em><code>private class JsToNative { \/\/ \u6ca1\u6709\u8fd4\u56de\u7ed3\u679c @JavascriptInterface public void jsMethod(String paramFromJS) { } \/\/ \u6709\u8fd4\u56de\u7ed3\u679c @JavascriptInterface public String jsMethodReturn(String paramFromJS) { return \"your result\"; } } \/\/ JsToNative\u5c31\u662f\u4e00\u4e2a\u522b\u540d\uff0c\u4f60\u53ef\u4ee5\u968f\u610f webView.addJavascriptInterface(new JsToNative(), \"JsToNative\");<\/code> JS \u8c03\u7528 Android <em><\/em><code>\/\/ \u6ca1\u6709\u8fd4\u56de\u7ed3\u679c var paramFromJS = \"xxx\"; window.JsToNative.jsMethod(paramFromJS); \/\/ \u6709\u8fd4\u56de\u7ed3\u679c var returnResult = window.JsToNative.jsMethodReturn(paramFromJS);<\/code><\/li><li>iOS<br>\u4e24\u79cd\u65b9\u5f0f\uff1a<ul><li>JS \u91cc\u9762\u76f4\u63a5\u8c03\u7528\u65b9\u6cd5<\/li><li>JS \u91cc\u9762\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u65b9\u6cd5<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>\u65b9\u5f0f\u4e00\uff1aJS \u91cc\u9762\u76f4\u63a5\u8c03\u7528\u65b9\u6cd5<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- (void)webViewDidFinishLoad:(UIWebView *)webView {\n    JSContext *context = &#91;webView valueForKeyPath:@\"documentView.webView.mainFrame.javaScriptContext\"];\n    context&#91;@\"jsMethod\"] = ^() { \n        NSArray *args = &#91;JSContext currentArguments];  \n        for (id obj in args) {  \n            NSLog(@\"%@\",obj);  \n        } \n    }\n\n    context&#91;@\"jsMethodReturn\"] = ^() { \n        return \"your result\";\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>JS \u8c03\u7528 iOS<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6ca1\u6709\u8fd4\u56de\u7ed3\u679c\nvar paramFromJS = \"xxx\";\njsMethod(paramFromJS);\n\n\/\/ \u6709\u8fd4\u56de\u7ed3\u679c\nvar returnResult = jsMethodReturn(paramFromJS);\n<\/code><\/pre>\n\n\n\n<p>\u65b9\u5f0f\u4e8c\uff1aJS \u91cc\u9762\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u65b9\u6cd5<\/p>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\u9700\u8981\u4f7f\u7528\u5230 JSExport \u534f\u8bae\uff0c\u7c7b\u4f3cAndroid\u7684 @JavascriptInterface \u6ce8\u89e3\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u7bc7\u5e45\u539f\u56e0\uff0c\u8fd9\u91cc\u4e0d\u505a\u8be6\u7ec6\u8bb2\u89e3\uff0c\u611f\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\"https:\/\/link.jianshu.com?t=http:\/\/blog.iderzheng.com\/ios7-objects-management-in-javascriptcore-framework\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u8fd9\u91cc<\/a>\u3002<\/p>\n\n\n\n<p>\u5c0f\u7ed3\uff1a<\/p>\n\n\n\n<ol><li>JS \u90fd\u53ef\u4ee5\u4ece Android \u548c iOS \u65b9\u6cd5\u62ff\u5230\u8fd4\u56de\u503c\uff0c\u4e0d\u5b58\u5728Android \u8c03\u7528 JS \u65e0\u6cd5\u62ff\u5230\u8fd4\u56de\u503c\u7684\u60c5\u51b5\u3002<\/li><li>\u5f0a\u7aef\u548c\u524d\u9762\u7c7b\u4f3c\uff0cJS \u4e8b\u5148\u9700\u8981\u77e5\u9053 Android \u548c iOS \u7684\u65b9\u6cd5\u540d\uff08\u53c2\u6570\u7b49\uff09\uff1b\u53e6\u5916\uff0c\u968f\u7740\u4e1a\u52a1\u7684\u589e\u957f\uff0c\u6211\u4eec\u4e0d\u5f97\u4e0d\u589e\u52a0\u66f4\u591a\u7684\u65b9\u6cd5\uff0c\u6765\u5904\u7406\u5176\u4ed6\u4e1a\u52a1\u3002<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u601d\u8003\uff1a<\/strong><\/p>\n\n\n\n<ol><li>\u5982\u4f55\u907f\u514d JS\u3001Android\u3001iOS \u76f8\u4e92\u8c03\u7528\u65f6\uff0c\u9700\u8981\u4e8b\u5148\u201c\u7ea6\u5b9a\u201d\u65b9\u6cd5\u540d\u79f0\u548c\u53c2\u6570\uff1f<\/li><li>\u539f\u751f\u8c03\u7528 JS \u65b9\u6cd5\uff0c\u80fd\u5426\u7c7b\u4f3c\u539f\u751f\u5f00\u53d1\u4e00\u6837\uff0c\u4f7f\u7528 <code>Callback\uff08block\uff09<\/code> \u505a\u4e3a\u56de\u8c03\u65b9\u5f0f\uff1f<\/li><li>JS \u8c03\u7528\u539f\u751f\u80fd\u5426\u4f7f\u7528 <code>function<\/code> \u83b7\u5f97\u8fd4\u56de\u503c?<\/li><\/ol>\n\n\n\n<p><strong>iOS\/OSX &#8211; WebViewJavascriptBridge<\/strong><\/p>\n\n\n\n<p>\u8fd9\u662f<a href=\"https:\/\/link.jianshu.com?t=https:\/\/github.com\/marcuswestin\" target=\"_blank\" rel=\"noreferrer noopener\">marcuswestin<\/a>\u516c\u53f8\u5f00\u6e90\u7684\u4e00\u4e2a\u7528\u4e8e iOS\/OSX \u5e73\u53f0 webview \u4e0e JS \u901a\u4fe1\u7684\u65b9\u6848\uff0c\u5b83\u5728 webview \u548c JS \u4e4b\u95f4\u201c\u67b6\u4e86\u201d\u4e00\u5ea7\u6865\u6881\uff0c\u63d0\u4f9b\u4e86\u975e\u5e38\u4fbf\u6377\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u5f15\u7528\u5b98\u65b9\u7684\u4ecb\u7ecd\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>An iOS\/OSX bridge for sending messages between Obj-C and JavaScript in UIWebViews\/WebViews<\/p><\/blockquote>\n\n\n\n<p>\u6b64\u9879\u76ee\u7684<strong>star<\/strong>\u6570\u91cf\u8fbe\u5230<strong>7600+<\/strong>\uff0c\u53ef\u89c1\u53d7\u6b22\u8fce\u7a0b\u5ea6\u975e\u5e38\u9ad8\uff0c\u800c\u4e14\u636e\u8bf4\u8fd8\u6709\u5f88\u591a\u5927\u516c\u53f8\u7684\u9879\u76ee\u5728\u4f7f\u7528\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/link.jianshu.com?t=https:\/\/www.facebook.com\/mobile\/messenger\" target=\"_blank\" rel=\"noreferrer noopener\">Facebook Messenger<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=https:\/\/facebook.com\/paper\" target=\"_blank\" rel=\"noreferrer noopener\">Facebook Paper<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=http:\/\/www.getyardsale.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Yardsale<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=http:\/\/www.evertrue.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">EverTrue<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=http:\/\/www.game-insight.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Game Insight<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=http:\/\/www.sush.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">Sush.io<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=http:\/\/imbed.github.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">Imbed<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=https:\/\/carezone.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">CareZone<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=http:\/\/www.hemlig.co\/\" target=\"_blank\" rel=\"noreferrer noopener\">Hemlig<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=http:\/\/www.altralogica.it\/\" target=\"_blank\" rel=\"noreferrer noopener\">Altralogica<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=https:\/\/itunes.apple.com\/us\/app\/ding-sheng-zhong-hua\/id537273940?mt=8\" target=\"_blank\" rel=\"noreferrer noopener\">\u9f0e\u76db\u4e2d\u534e<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=https:\/\/fril.jp\/\" target=\"_blank\" rel=\"noreferrer noopener\">FRIL<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=http:\/\/liubaiapp.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u7559\u767d\u00b7WHITE<\/a><\/li><li><a href=\"https:\/\/link.jianshu.com?t=http:\/\/thirdiron.com\/browzine\/\" target=\"_blank\" rel=\"noreferrer noopener\">BrowZine<\/a><\/li><\/ul>\n\n\n\n<p>\u8fd9\u4e2a\u5f00\u6e90\u9879\u76ee\u7684\u7528\u6cd5\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u7b80\u5355\u7684\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol><li>\u6ce8\u518chandler<\/li><\/ol>\n\n\n\n<p>Obj-C\u4e2d\u6ce8\u518chandler\uff0c\u7ed9JS\u8c03\u7528\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   self.bridge = &#91;WebViewJavascriptBridge bridgeForWebView:webView];\n   &#91;self.bridge registerHandler:@\"testObjcCallback\" handler:^(id data, WVJBResponseCallback responseCallback) {\n      \/\/ \u6536\u5230JS\u7684\u8c03\u7528\n      NSLog(@\"testObjcCallback called: %@\", data);\n      \/\/ \u56de\u8c03\u7ed3\u679c\u7ed9JS\n      responseCallback(@\"Response from testObjcCallback\");\n   }];\n<\/code><\/pre>\n\n\n\n<p>JS\u4e2d\u6ce8\u518chandler\uff0c\u7ed9Obj-C\u8c03\u7528\uff08JS\u4ee3\u7801\uff09\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   bridge.registerHandler('testJavascriptHandler', function(data, responseCallback) {\n      \/\/ \u6536\u5230Obj-C\u7684\u8c03\u7528\n      log('ObjC called testJavascriptHandler with', data)\n      var responseData = { 'Javascript Says':'Right back atcha!' }\n      log('JS responding with', responseData)\n      \/\/ \u56de\u8c03\u7ed3\u679c\u7ed9Obj-C\n      responseCallback(responseData)\n   })\n<\/code><\/pre>\n\n\n\n<ol start=\"2\"><li>\u6839\u636e\u7b2c\u4e00\u6b65\u6ce8\u518c\u7684<code>handler<\/code>\uff0c\u53d1\u9001\u6d88\u606f<br>\u7b2c\u4e00\u6b65\u6ce8\u518c\u7684<code>handler<\/code>\u6709\u4e24\u4e2a\uff1a<code>testObjcCallback<\/code>\u548c<code>testJavascriptHandler<\/code><\/li><\/ol>\n\n\n\n<p>Obj-C\u8c03\u7528JS\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   &#91;self.bridge callHandler:@\"testJavascriptHandler\" data:data responseCallback:^(id response) {\n      NSLog(@\"testJavascriptHandler responded: %@\", response);\n   }];\n<\/code><\/pre>\n\n\n\n<p>JS\u8c03\u7528Obj-C\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   bridge.callHandler('testObjcCallback', {'foo': 'bar'}, function(response) {\n      log('JS got response', response)\n   })\n<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u4e3b\u8981\u5c31\u662f\u4f7f\u7528<code>registerHandler<\/code>\u6765\u6ce8\u518c<code>callback\uff08block\uff09<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>callHandler<\/code>\u6765\u8c03\u7528\u6ce8\u518c\u7684<code>callback\uff08block\uff09<\/code>\u3002<\/p>\n\n\n\n<p>Obj-C\u4e0eJS\u4e92\u8c03\uff0c\u4f20\u9012\u6570\u636e\u7684\u683c\u5f0f\u4e3aString\uff0c\u5efa\u8bae\u4f7f\u7528JSON\u683c\u5f0f\uff0c\u8fd9\u6837\u66f4\u6613\u4e8e\u6570\u636e\u7684\u4ea4\u4e92\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u5173\u4e8eWebViewJavascriptBridge\u66f4\u591a\u8be6\u7ec6\u7684\u914d\u7f6e\u548c\u7528\u6cd5\uff0c<a href=\"https:\/\/link.jianshu.com?t=https:\/\/github.com\/marcuswestin\/WebViewJavascriptBridge\" target=\"_blank\" rel=\"noreferrer noopener\">\u8bf7\u770b\u8fd9\u91cc<\/a>\u3002<\/p><\/blockquote>\n\n\n\n<p>iOS\/OSX\u5e73\u53f0\u6709\u8fd9\u6837\u7684Obj-C\u4e0eJS\u4ea4\u4e92\u65b9\u6848\uff0c\u5982\u679cAndroid\u5e73\u53f0\u4e5f\u6709\u7c7b\u4f3c\u7684\u65b9\u6848\uff0c\u5c82\u4e0d\u662f\u66f4\u52a0\u5b8c\u7f8e\uff1f<\/p>\n\n\n\n<p><strong>Android &#8211; JsBridge<\/strong><\/p>\n\n\n\n<p>\u611f\u8c22\u5f00\u6e90\u7684\u529b\u91cf\uff0c\u5df2\u7ecf\u6709\u4eba\u7ed9\u51fa\u4e86\u7c7b\u4f3c\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n\n\n\n<p>hi\u5927\u5934\u9b3chi\uff08lzyzsd\uff09\uff0c\u5fae\u535a\u5730\u5740\uff1a<a href=\"https:\/\/link.jianshu.com?t=http:\/\/weibo.com\/brucefromsdu\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/weibo.com\/brucefromsdu<\/a><br>JsBridge \u5f00\u6e90\u5730\u5740\uff1a<a href=\"https:\/\/link.jianshu.com?t=https:\/\/github.com\/lzyzsd\/JsBridge\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/lzyzsd\/JsBridge<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u636e\u4f5c\u8005\u63cf\u8ff0\uff0c\u662f\u4ece<a href=\"https:\/\/link.jianshu.com?t=https:\/\/github.com\/jacin1\/JsBridge\" target=\"_blank\" rel=\"noreferrer noopener\">JsBridge<\/a>\u548c\u5fae\u4fe1\u7684jsBridge file\u6539\u9020\u800c\u6765\uff0c\u52a0\u4e86\u4e00\u4e9b\u65b0\u7279\u6027\u548c\u4fee\u590d\u4e86\u4e00\u4e9bbug\u3002<\/p><\/blockquote>\n\n\n\n<p>\u9605\u8bfb\u5b8c\u6574\u4e2a\u5f00\u6e90\u9879\u76ee\u4e4b\u540e\uff0c\u6211\u60ca\u8bb6\u7684\u53d1\u73b0\uff0c\u5173\u4e8e <strong>jsBridge<\/strong> \u7684\u8bbe\u8ba1\u5c45\u7136\u548c\u4e0a\u6587\u4ecb\u7ecd\u7684 <strong>WebViewJavascriptBridge<\/strong> \u51e0\u4e4e\u4e00\u6a21\u4e00\u6837\u3002<\/p>\n\n\n\n<p>\u552f\u4e00\uff0c\u4e5f\u662f\u6700\u660e\u663e\u7684\u4e00\u4e2a\u533a\u522b\uff0c\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u6211\u4eec\u672c\u6587\u5f00\u59cb\u9047\u5230\u7684\u95ee\u9898\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Android &amp; iOS \u8c03\u7528 JS \u7684\u65b9\u6cd5 &#8211; Android \u6ca1\u6cd5\u62ff\u5230\u8fd4\u56de\u503c\uff1b\u4f46\u662f\uff0ciOS\u662f\u53ef\u4ee5\u62ff\u5230\u8fd4\u56de\u503c\u7684\u3002<\/p><\/blockquote>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f5c\u8005\u4f7f\u7528\u4e86 <strong>webview url<\/strong> \u81ea\u5b9a\u4e49\u7684 <strong>schema<\/strong> \uff0c\u7136\u540e\u622a\u53d6\u6570\u636e\u5e76\u62e6\u622a\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p><strong>jsBridge<\/strong> \u5dee\u5f02\u90e8\u5206\uff0c\u5173\u952e\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul><li>iOS &#8211; jsBridge <em><\/em><code>function _fetchQueue() { var messageQueueString = JSON.stringify(sendMessageQueue); sendMessageQueue = []; return messageQueueString; }<\/code><\/li><li>Android &#8211; jsBridge <em><\/em><code>function _fetchQueue() { var messageQueueString = JSON.stringify(sendMessageQueue); sendMessageQueue = []; \/\/ return messageQueueString; \/\/ Android\u65e0\u6cd5\u76f4\u63a5\u8fd4\u56de\u6570\u636e, \u8fd9\u662f\u4e0eiOS\u6700\u5927\u7684\u533a\u522b; \u6240\u4ee5, \u9700\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49url\u5f62\u5f0f\u8fd4\u56de\u6570\u636e\u3002 messagingIframe.src = CUSTOM_PROTOCOL_SCHEME + ':\/\/return\/_fetchQueue\/' + encodeURIComponent(messageQueueString); }<\/code><\/li><\/ul>\n\n\n\n<p>\u53e6\u5916\uff0c<strong>jsBridge<\/strong> \u7684\u201c\u52a0\u8f7d\u65f6\u673a\u201d\u4e5f\u6709\u6240\u4e0d\u540c\uff0c\u5dee\u5f02\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul><li>iOS &#8211; WebViewJavascriptBridge <em><\/em><code>function setupWebViewJavascriptBridge(callback) { if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); } if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); } window.WVJBCallbacks = [callback]; var WVJBIframe = document.createElement('iframe'); WVJBIframe.style.display = 'none'; WVJBIframe.src = 'wvjbscheme:\/\/__BRIDGE_LOADED__'; document.documentElement.appendChild(WVJBIframe); setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0) }<\/code><\/li><\/ul>\n\n\n\n<p>iOS\u4e2d\uff0c\u9700\u8981\u5728\u4f60\u7684web\u9875\u9762\u7684JS\u811a\u672c\u4e2d\u6267\u884c\u8fd9\u4e48\u4e00\u6bb5\u65b9\u6cd5\uff0c\u7136\u540e\u5728UIWebView\u7684\u4ee3\u7406\u65b9\u6cd5<code>shouldStartLoadWithRequest<\/code>\u62e6\u622a<code>wvjbscheme:\/\/__BRIDGE_LOADED__<\/code>\u7c7b\u578b\u7684URL\uff0c\u7136\u540e\u52a0\u8f7d jsBridge\uff1a<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"\/\/upload-images.jianshu.io\/upload_images\/1787010-3ba6192c14c299a0.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/627\/format\/webp\" alt=\"\"\/><\/figure>\n\n\n\n<p>iOS &#8211; jsBridge.png<\/p>\n\n\n\n<ul><li>Android &#8211; JsBridge<br>\u800cAndroid\u4e2d\uff0c\u4e0d\u9700\u8981\u5728web\u9875\u9762\u5f15\u5165\u90a3\u6bb5JS\u65b9\u6cd5\uff1b\u53ea\u9700\u8981\u5728WebViewClient\u7684<code>onPageFinished<\/code>\u65b9\u6cd5\u4e2d\u52a0\u8f7d jsBridge\uff1a<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"\/\/upload-images.jianshu.io\/upload_images\/1787010-984f949ca06a4c4a.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/521\/format\/webp\" alt=\"\"\/><\/figure>\n\n\n\n<p>Android &#8211; jsBridge.png<\/p>\n\n\n\n<p>\u5f88\u660e\u663e\uff0cAndroid \u548c iOS \u4e0d\u540c\u7684\u5e73\u53f0\u9700\u8981\u5728 web \u9875\u9762\u5f15\u5165\u7684\u5185\u5bb9\u6709\u6240\u4e0d\u4e00\u6837\uff1b\u8fd9\u6837\uff0c\u4f1a\u5bfc\u81f4 web \u9875\u9762\u5f00\u53d1\u4eba\u5458\u9700\u8981\u6839\u636e\u4e0d\u540c\u7684\u5e73\u53f0\u5206\u522b\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u65e2\u7136\uff0c\u6211\u4eec\u7684\u521d\u8877\u662f\u60f3\u627e\u5230 Android \u548c iOS \u5e73\u53f0\u540c\u65f6\u90fd\u9002\u7528\u7684\u65b9\u6848\uff0c\u5c31\u5fc5\u987b\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>\u4e3a\u6b64\uff0c\u6211\u6539\u9020\u4e86 \u5927\u5934\u9b3c \u7684JsBridge\u5e93<\/strong>\uff0cAndroid \u4f7f\u7528\u4e86\u548c iOS &#8211; WebViewJavascriptBridge \u7684\u4e00\u81f4\u7684 jsBridge \u6587\u4ef6\uff0c\u552f\u4e00\u4fee\u6539\u7684\u65b9\u6cd5\u662f<code>_fetchQueue()<\/code>\uff0c\u6b63\u5982\u524d\u9762\u63d0\u5230\u7684\uff0c\u4e3a\u4e86\u89e3\u51b3\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Android &amp; iOS \u8c03\u7528 JS \u7684\u65b9\u6cd5 &#8211; Android \u6ca1\u6cd5\u62ff\u5230\u8fd4\u56de\u503c\uff1b\u4f46\u662f\uff0ciOS\u662f\u53ef\u4ee5\u62ff\u5230\u8fd4\u56de\u503c\u7684\u3002<\/p><\/blockquote>\n\n\n\n<p>\u540c\u65f6\uff0c\u9700\u8981\u548c iOS \u4e00\u6837\uff0c\u5728 web \u9875\u9762\u6267\u884c\u4e00\u6bb5 JS \u65b9\u6cd5\u3002\u5176\u4ed6\u4f7f\u7528\u65b9\u5f0f\u57fa\u672c\u4fdd\u6301\u4e0d\u53d8\uff0c\u8fd9\u91cc\u611f\u8c22 \u5927\u5934\u9b3c \u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<p><strong>\u672c\u6765\u662f\u4ecefork\u8fc7\u6765\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u6539\u52a8\u6bd4\u8f83\u591a\uff0c\u6240\u4ee5\u5c31\u4e0d\u63d0\u4ea4\u7ed9\u539f\u4f5c\u8005\u4e86\u3002<\/strong><\/p>\n\n\n\n<p>\u57fa\u672c\u4f7f\u7528\u6b65\u9aa4\u548ciOS\u7684\u4fdd\u6301\u9ad8\u5ea6\u4e00\u81f4\uff0c\u7b80\u5355\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol><li>\u6ce8\u518chandler<\/li><\/ol>\n\n\n\n<p>Android\u4e2d\u6ce8\u518chandler\uff0c\u7ed9JS\u8c03\u7528\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   webView = (BridgeWebView) findViewById(R.id.webView);\n   webView.registerHandler(\"testObjcCallback\", new BridgeHandler() {\n      @Override\n      public void handler(String data, CallBackFunction function) {\n         Log.i(TAG, \"testObjcCallback called: \" + data);\n         function.onCallBack(\"Response from testObjcCallback\");\n      }\n   });\n<\/code><\/pre>\n\n\n\n<p>JS\u4e2d\u6ce8\u518chandler\uff0c\u7ed9Android\u8c03\u7528\uff08JS\u4ee3\u7801\uff09\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   bridge.registerHandler('testJavascriptHandler', function(data, responseCallback) {\n      log('ObjC called testJavascriptHandler with', data)\n    var responseData = { 'Javascript Says':'Right back atcha!' }\n    log('JS responding with', responseData)  \n      responseCallback(responseData)\n   })\n<\/code><\/pre>\n\n\n\n<ol start=\"2\"><li>\u6839\u636e\u7b2c\u4e00\u6b65\u6ce8\u518c\u7684<code>handler<\/code>\uff0c\u53d1\u9001\u6d88\u606f<br>\u7b2c\u4e00\u6b65\u6ce8\u518c\u7684<code>handler<\/code>\u6709\u4e24\u4e2a\uff1a<code>testObjcCallback<\/code>\u548c<code>testJavascriptHandler<\/code><\/li><\/ol>\n\n\n\n<p>Android\u8c03\u7528JS\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   webView.callHandler(\"testJavascriptHandler\", \"{\\\"foo\\\":\\\"before ready\\\"}\", new CallBackFunction() {\n      @Override\n      public void onCallBack(String data) {\n\n      }\n   });\n<\/code><\/pre>\n\n\n\n<p>JS\u8c03\u7528Android\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   bridge.callHandler('testObjcCallback', {'foo': 'bar'}, function(response) {\n      log('JS got response', response)\n})\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u5173\u4e8eJsBridge\u66f4\u591a\u8be6\u7ec6\u7684\u914d\u7f6e\u548c\u7528\u6cd5\uff0c<a href=\"https:\/\/link.jianshu.com?t=https:\/\/github.com\/hiphonezhu\/JsBridge\" target=\"_blank\" rel=\"noreferrer noopener\">\u8bf7\u770b\u8fd9\u91cc<\/a>\u3002<\/p><\/blockquote>\n\n\n\n<p>\u6709\u4e86\u8fd9\u6837\u7684\u4e24\u4e2a\u5e93\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u8ba9\u6211\u4eec\u5728Android\u548ciOS\u5e73\u53f0\u4e0eJS\u4ea4\u4e92\u65f6\uff0c\u4f7f\u7528\u4e00\u81f4\u7684\u901a\u4fe1\u65b9\u6848\uff0c\u8f83\u5c11\u4e86\u524d\u7aef\u540c\u5b66\u5f88\u591a\u7684\u9002\u914d\u5de5\u4f5c\u3002\u540c\u65f6\uff0c\u4e5f\u66f4\u7b26\u5408\u539f\u751f\u7684\u5f00\u53d1\u4e60\u60ef\uff0c\u5e76\u4e14\u5ffd\u7565 jsBridge \u5c42\u7684\u5b58\u5728\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<p>1.\u57fa\u4e8ewebview\u7684 &#8212;&#8211;<\/p>\n\n\n\n<p>1-1.ifram\uff0c\u521b\u5efa\u7a7a\u6a21\u5757\u53d1\u9001\u6307\u5b9aurl\u5e26\u53c2\u6570\u4f20\u9012\u7ed9\u539f\u751f\uff0c\u5ba2\u6237\u7aef\u901a\u8fc7\u62e6\u622a\u8fd9\u4e2a\u8bf7\u6c42\uff08\u7ed3\u5408jsBridge\uff09<\/p>\n\n\n\n<p>1-2.localtion.href = xxx\u5e26\u53c2\u6570\u4f20\u8fc7\u53bb(\u5982\u679c\u8fde\u7eed\u591a\u6b21\u4fee\u6539,\u539f\u751f\u5c42\u53ea\u80fd\u63a5\u6536\u5230\u6700\u540e\u4e00\u6b21\u8bf7\u6c42\uff0c\u524d\u9762\u7684\u8bf7\u6c42\u90fd\u4f1a\u88ab\u5ffd\u7565\u6389\u3002)<\/p>\n\n\n\n<p>2.jsBridge-\u62e6\u622a\u8bf7\u6c42\uff0c\u89e3\u6790\u8bf7\u6c42\uff0c\u8fd4\u56de\u6570\u636e(\u4e09\u7aef\u5c01\u88c5)<\/p>\n\n\n\n<p>3.\u8de8\u9875\u9762\u4f20\u9012\uff0cwindow.parent.postMassage\u642d\u914dmessage\uff0c\u6302\u5728window\u5bf9\u8c61\uff0c\u65e0\u8de8\u57df\u9650\u5236(\u642d\u914difram)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>webview \u4e0e JS \u4ea4\u4e92\u5206\u4e3a\u4e24\u79cd\uff1a Android &amp; iOS \u8c03\u7528 JS \u7684\u65b9\u6cd5 JS \u8c03\u7528  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,1],"tags":[19],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/1144"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1144"}],"version-history":[{"count":1,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/1144\/revisions"}],"predecessor-version":[{"id":1145,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/1144\/revisions\/1145"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1144"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}