{"id":497,"date":"2023-02-25T14:15:40","date_gmt":"2023-02-25T06:15:40","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=497"},"modified":"2023-04-29T20:49:26","modified_gmt":"2023-04-29T12:49:26","slug":"react-native-learning-webview-component","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-learning-webview-component\/","title":{"rendered":"React Native\u5b66\u4e60\u4e4bWebView\u7ec4\u4ef6"},"content":{"rendered":"<p><code>Hybrid App<\/code>\uff08\u6df7\u5408\u6a21\u5f0f\u79fb\u52a8\u5e94\u7528\uff09\u662f\u6307\u4ecb\u4e8e<code>web-app<\/code>\u3001<code>native-app<\/code>\u8fd9\u4e24\u8005\u4e4b\u95f4\u7684app\uff0c\u517c\u5177\u201cNative App\u826f\u597d\u7528\u6237\u4ea4\u4e92\u4f53\u9a8c\u7684\u4f18\u52bf\u201d\u548c\u201cWeb App\u8de8\u5e73\u53f0\u5f00\u53d1\u7684\u4f18\u52bf\u201d\u3002<\/p>\n<p>\u91cd\u70b9\u7406\u89e3\uff1a\u4e3a\u4ec0\u4e48rn\u4e2d\u4f1a\u6709WebView\uff1f<\/p>\n<h3>WebView\u7ec4\u4ef6\u7684\u5c5e\u6027<\/h3>\n<p><!-- more --><\/p>\n<ul>\n<li><code>automaticallyAdjustContentInsets<\/code>\uff1a\u662f\u5426\u81ea\u52a8\u8c03\u6574\u5185\u90e8\u5185\u5bb9\u3002<\/li>\n<li><code>bounces(IOS)<\/code>\uff1a\u56de\u5f39\u6548\u679c \u5982\u679c\u4e3afalse\uff0c\u5219\u5185\u5bb9\u62c9\u5230\u5e95\u90e8\u6216\u5934\u90e8\u4e0d\u56de\u5f39\uff0c\u9ed8\u8ba4\u4e3atrue\u3002<\/li>\n<li><code>domStorageEnabled(Android)<\/code>\uff1a\u4ec5\u9650Android\u5e73\u53f0\u3002\u6307\u5b9a\u662f\u5426\u5f00\u542fDOM\u672c\u5730\u5b58\u50a8\u3002<\/li>\n<li><code>javaScriptEnabled<\/code>\uff1a\u4ec5\u9650Android\u5e73\u53f0\u3002iOS\u5e73\u53f0JavaScript\u662f\u9ed8\u8ba4\u5f00\u542f\u7684\u3002<\/li>\n<li><code>contentInset<\/code>\uff1a\u5185\u90e8\u5185\u5bb9\u504f\u79fb\u503c\uff0c\u8be5\u503c\u4e3a\u4e00\u4e2aJavaScript\u5bf9\u8c61<code>{% raw %}{top:number,left:number,bottom:number,right:number}{% endraw %}<\/code><\/li>\n<li><code>source<\/code>\uff1a<code>{% raw %}{{uri:&#039;\u7f51\u5740&#039;}}{% endraw %}<\/code>\u5728WebView\u4e2d\u8f7d\u5165\u4e00\u6bb5\u9759\u6001\u7684html\u4ee3\u7801\u6216\u662f\u4e00\u4e2aurl(\u8fd8\u53ef\u4ee5\u9644\u5e26\u4e00\u4e9bheader\u9009\u9879)\uff1b<code>{% raw %}{{html:&#039;html\u4ee3\u7801\u5757&#039;}}{% endraw %}<\/code>\u3002<\/li>\n<li><code>injectedJavaScript<\/code>\uff1a\u8bbe\u7f6e\u5728Webview\u7f51\u9875\u52a0\u8f7d\u4e4b\u524d\u6ce8\u5165\u7684\u4e00\u6bb5JS\u4ee3\u7801\uff0c\u5176\u503c\u4e3a\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>mediaPlaybackRequiresUserAction<\/code>\uff1a\u8bbe\u7f6e\u9875\u9762\u4e2d\u7684HTML5\u97f3\u89c6\u9891\u662f\u5426\u9700\u8981\u5728\u7528\u6237\u70b9\u51fb\u540e\u518d\u5f00\u59cb\u64ad\u653e\uff0c\u9ed8\u8ba4\u503c\u4e3afalse\u3002<\/li>\n<li><code>startInLoadingState<\/code>\uff1a\u662f\u5426\u5f00\u542f\u9875\u9762\u52a0\u8f7d\u7684\u72b6\u6001\u3002<\/li>\n<li><code>scrollEnabled(IOS)<\/code>\uff1a\u8868\u793aWebview\u91cc\u9762\u9875\u9762\u662f\u5426\u80fd\u6eda\u52a8\uff0c\u5982\u679c\u5176\u503c\u4e3atrue\u5219\u53ef\u4ee5\u6eda\u52a8\uff0c\u5426\u5219\u7981\u6b62\u6eda\u52a8\u3002<\/li>\n<li><code>scalesPageToFit<\/code>\uff1a\u6309\u7167\u9875\u9762\u6bd4\u4f8b\u548c\u5185\u5bb9\u5bbd\u9ad8\u6bd4\u4f8b\u81ea\u52a8\u7f29\u653e\u5185\u5bb9\u3002<\/li>\n<li><code>userAgent<\/code>\uff1a\u4e3aWebView\u8bbe\u7f6euser-agent\u5b57\u7b26\u4e32\u6807\u8bc6\u3002\u8fd9\u4e00\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\u5728\u539f\u751f\u7aef\u7528WebViewConfig\u6765\u8bbe\u7f6e\uff0c\u4f46js\u7aef\u7684\u8bbe\u7f6e\u4f1a\u8986\u76d6\u539f\u751f\u7aef\u7684\u8bbe\u7f6e\u3002<\/li>\n<\/ul>\n<h3>WebView\u7ec4\u4ef6\u7684\u65b9\u6cd5<\/h3>\n<ul>\n<li><code>onError<\/code>\uff1a\u52a0\u8f7d\u5931\u8d25\u65f6\u8c03\u7528\u3002<\/li>\n<li><code>onLoad<\/code>\uff1a\u52a0\u8f7d\u6210\u529f\u65f6\u8c03\u7528\u3002<\/li>\n<li><code>onLoadEnd<\/code>\uff1a\u52a0\u8f7d\u7ed3\u675f\u65f6(\u65e0\u8bba\u6210\u529f\u6216\u5931\u8d25)\u8c03\u7528\u3002<\/li>\n<li><code>onLoadStart<\/code>\uff1a\u52a0\u8f7d\u5f00\u59cb\u65f6\u8c03\u7528\u3002<\/li>\n<li><code>onNavigationStateChange<\/code>\uff1a\u76d1\u542c\u5bfc\u822a\u72b6\u6001\u53d8\u5316\u7684\u51fd\u6570(\u5f53\u53d1\u73b0\u6d4f\u89c8\u5668\u5730\u5740\u6539\u53d8\u65f6\uff0c\u89e6\u53d1\u4e8b\u4ef6)<\/li>\n<li><code>onShouldStartLoadWithRequest<\/code>\uff1a\u4ec5\u9650iOS\u5e73\u53f0\u3002\u5141\u8bb8\u4e3aWebview\u53d1\u8d77\u7684\u8bf7\u6c42\u8fd0\u884c\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u5904\u7406\u51fd\u6570\u3002\u8fd4\u56detrue\u6216false\u8868\u793a\u662f\u5426\u8981\u7ee7\u7eed\u6267\u884c\u54cd\u5e94\u7684\u8bf7\u6c42\u3002<\/li>\n<li><code>renderError<\/code>\uff1a\u76d1\u542c\u6e32\u67d3\u9875\u9762\u51fa\u9519\u7684\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u89c6\u56fe\u7528\u4e8e\u663e\u793a\u9519\u8bef\u3002<\/li>\n<li><code>renderLoading<\/code>\uff1aWebview\u7ec4\u4ef6\u6b63\u5728\u6e32\u67d3\u9875\u9762\u65f6\u89e6\u53d1\u7684\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u52a0\u8f7d\u6307\u793a\u5668\u3002\u9700\u8981\u540cstartInLoadingState\u4e00\u8d77\u4f7f\u7528\uff0c\u5f53startInLoadingState\u4e3atrue\u65f6\u8be5\u51fd\u6570\u624d\u8d77\u4f5c\u7528<\/li>\n<\/ul>\n<p>\u8303\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n *\/\n\nimport React, { Component } from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    Text,\n    Image,\n    WebView,\n    View,\n} from &#039;react-native&#039;;\n\nimport Dimensions from &#039;Dimensions&#039;;\n\nconst width = Dimensions.get(&#039;window&#039;).width;\nconst height = Dimensions.get(&#039;window&#039;).height;\n\nclass RNAPP extends Component{\n    constructor(props) {\n        super(props);\n    }\n\n    render() {\n        return(\n            &lt;View style={styles.flex}&gt;\n                &lt;WebView\n                    style={{width: width, height: height}}\n                    source={{uri: &#039;http:\/\/www.rnapp.cc&#039;}}\n                    \/\/injectedJavaScript={&#039;alert(&quot;RNAPP.CC&quot;)&#039;}\n                    \/\/source={{html: &#039;&lt;div&gt;&lt;img scr=&quot;http:\/\/www.rnapp.cc\/webview_test.jpg&quot; \/&gt;&lt;\/div&gt;&#039;}}\n                    &gt;\n                &lt;\/WebView&gt;\n            &lt;\/View&gt;\n        );\n    }\n}\n\nconst styles = StyleSheet.create({\n    flex: {\n        flex: 1;\n    },\n});\n\nAppRegistry.registerComponent(&#039;RNAPP&#039;, () =&gt; RNAPP);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hybrid App\uff08\u6df7\u5408\u6a21\u5f0f\u79fb\u52a8\u5e94\u7528\uff09\u662f\u6307\u4ecb\u4e8eweb-app\u3001native-app\u8fd9\u4e24\u8005\u4e4b\u95f4\u7684app\uff0c\u517c\u5177\u201c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[],"class_list":["post-497","post","type-post","status-publish","format-standard","hentry","category-react-native"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=497"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/497\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}