WebView
Warning Please use the react-native-community/react-native-webview fork of this component instead. To reduce the surface area of React Native,
<WebView/>
is going to be removed from the React Native core. For more information, please read The Slimmening proposal.
WebView
创建一个原生的 WebView,可以用于访问一个网页。
import React, { Component } from 'react';
import { WebView } from 'react-native';
class MyWeb extends Component {
render() {
return (
<WebView
source={{uri: 'https://github.com/facebook/react-native'}}
style={{marginTop: 20}}
/>
);
}
}
还可以直接嵌入 html 代码:
import React, { Component } from 'react';
import { WebView } from 'react-native';
class MyInlineWeb extends Component {
render() {
return (
<WebView
originWhitelist={['*']}
source={{ html: '<h1>Hello world</h1>' }}
/>
);
}
}
你可以使用这个组件进行网页的来回导航,并且为网页内容设置多方面的属性。
On iOS, the useWebKit
prop can be used to opt into a WKWebView-backed implementation.
安全提示: 目前,
onMessage
andpostMessage
方法不能够指定源。当WebView
加载某些非预期文档时可能导致跨站脚本攻击。请查阅 MDN 文档获取更多安全方面的细节Window.postMessage()
.
查看 Props
allowsInlineMediaPlayback
allowFileAccess
allowUniversalAccessFromFileURLs
automaticallyAdjustContentInsets
bounces
contentInset
dataDetectorTypes
decelerationRate
domStorageEnabled
geolocationEnabled
html
injectedJavaScript
injectJavaScript
javaScriptEnabled
mediaPlaybackRequiresUserAction
mixedContentMode
nativeConfig
onError
onLoad
onLoadEnd
onLoadStart
onMessage
onNavigationStateChange
onShouldStartLoadWithRequest
originWhitelist
renderError
renderLoading
scalesPageToFit
scrollEnabled
startInLoadingState
source
style
thirdPartyCookiesEnabled
useWebKit
userAgent
url
查看方法
文档
Props
allowUniversalAccessFromFileURLs
Boolean that sets whether JavaScript running in the context of a file scheme URL should be allowed to access content from any origin. Including accessing content from other file scheme URLs. The default value is false
.
类型 | 必需 | 平台 |
---|---|---|
bool | 否 | Android |
allowFileAccess
Boolean that sets whether the WebView
has access to the file system. The default value is false
.
类型 | Required | 平台 |
---|---|---|
bool | No | Android |
geolocationEnabled
Set whether Geolocation is enabled in the WebView
. The default value is false
. Used only in Android.
类型 | 必需 | 平台 |
---|---|---|
bool | 否 | Android |
source
在 WebView 中载入一段静态的 html 代码或是一个 url(还可以附带一些 header 选项)。注意如果是载入 html 代码,则需要设置originWhitelist
,比如可以设为["*"]
来允许运行本地代码。
The object passed to source
can have either of the following shapes:
Load uri
uri
(string) - The URI to load in theWebView
. Can be a local or remote file.method
(string) - The HTTP Method to use. Defaults to GET if not specified. On Android, the only supported methods are GET and POST.headers
(object) - Additional HTTP headers to send with the request. On Android, this can only be used with GET requests.body
(string) - The HTTP body to send with the request. This must be a valid UTF-8 string, and will be sent exactly as specified, with no additional encoding (e.g. URL-escaping or base64) applied. On Android, this can only be used with POST requests.
Static HTML
html
(string) - A static HTML page to display in the WebView.baseUrl
(string) - The base URL to be used for any relative links in the HTML.
类别 | 必需 |
---|---|
object | No |