66 KiB
id, title, original_id
| id | title | original_id |
|---|---|---|
| version-0.30-webview | webview | webview |
WebView # | Edit on GitHub |
WebView renders web content in a native view.
class MyWeb extends Component { render() { return ( <WebView source={{uri: 'https://github.com/facebook/react-native'}} style={{marginTop: 20}} /> ); } }
You can use this component to navigate back and forth in the web view's history and configure various properties for the web content.
Props #
automaticallyAdjustContentInsets bool #
Controls whether to adjust the content inset for web views that are
placed behind a navigation bar, tab bar, or toolbar. The default value
is true.
contentInset {top: number, left: number, bottom: number, right: number} #
The amount by which the web view content is inset from the edges of the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.
html string #
injectedJavaScript string #
Set this to provide JavaScript that will be injected into the web page when the view loads.
mediaPlaybackRequiresUserAction bool #
Boolean that determines whether HTML5 audio and video requires the user
to tap them before they start playing. The default value is false.
onError function #
Function that is invoked when the WebView load fails.
onLoad function #
Function that is invoked when the WebView has finished loading.
onLoadEnd function #
Function that is invoked when the WebView load succeeds or fails.
onLoadStart function #
Function that is invoked when the WebView starts loading.
onNavigationStateChange function #
Function that is invoked when the WebView loading starts or ends.
renderError function #
Function that returns a view to show if there's an error.
renderLoading function #
Function that returns a loading indicator.
scalesPageToFit bool #
Boolean that controls whether the web content is scaled to fit
the view and enables the user to change the scale. The default value
is true.
source {uri: string, method: string, headers: object, body: string}, {html: string, baseUrl: string}, number #
Loads static html or a uri (with optional headers) in the WebView.
startInLoadingState bool #
Boolean value that forces the WebView to show the loading view
on the first load.
style View#style #
The style to apply to the WebView.
url string #
androiddomStorageEnabled bool #
Boolean value to control whether DOM Storage is enabled. Used only in Android.
androidjavaScriptEnabled bool #
Boolean value to enable JavaScript in the WebView. Used on Android only
as JavaScript is enabled by default on iOS. The default value is true.
androiduserAgent string #
Sets the user-agent for the WebView.
iosallowsInlineMediaPlayback bool #
Boolean that determines whether HTML5 videos play inline or use the
native full-screen controller. The default value is false.
NOTE : In order for video to play inline, not only does this
property need to be set to true, but the video element in the HTML
document must also include the webkit-playsinline attribute.
iosbounces bool #
Boolean value that determines whether the web view bounces
when it reaches the edge of the content. The default value is true.
iosdecelerationRate ScrollView.propTypes.decelerationRate #
A floating-point number that determines how quickly the scroll view
decelerates after the user lifts their finger. You may also use the
string shortcuts "normal" and "fast" which match the underlying iOS
settings for UIScrollViewDecelerationRateNormal and
UIScrollViewDecelerationRateFast respectively:
- normal: 0.998
- fast: 0.99 (the default for iOS web view)
iosonShouldStartLoadWithRequest function #
Function that allows custom handling of any web view requests. Return
true from the function to continue loading the request and false
to stop loading.
iosscrollEnabled bool #
Boolean value that determines whether scrolling is enabled in the
WebView. The default value is true.
Methods #
Examples # | Edit on GitHub |
var React = require('react'); var ReactNative = require('react-native'); var { StyleSheet, Text, TextInput, TouchableWithoutFeedback, TouchableOpacity, View, WebView } = ReactNative;
var HEADER = '#3b5998'; var BGWASH = 'rgba(255,255,255,0.8)'; var DISABLED_WASH = 'rgba(255,255,255,0.25)';
var TEXT_INPUT_REF = 'urlInput'; var WEBVIEW_REF = 'webview'; var DEFAULT_URL = 'https://m.facebook.com';
var WebViewExample = React.createClass({
getInitialState: function() { return { url: DEFAULT_URL, status: 'No Page Loaded', backButtonEnabled: false, forwardButtonEnabled: false, loading: true, scalesPageToFit: true, }; },
inputText: '',
handleTextInputChange: function(event) { var url = event.nativeEvent.text; if (!/^[a-zA-Z-_]+:/.test(url)) { url = 'http://' + url; } this.inputText = url; },
render: function() { this.inputText = this.state.url;
<span class="token keyword">return</span> <span class="token punctuation">(</span>
<View style<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">[</span>styles<span class="token punctuation">.</span>container<span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token operator">></span>
<View style<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">[</span>styles<span class="token punctuation">.</span>addressBarRow<span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token operator">></span>
<TouchableOpacity
onPress<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>goBack<span class="token punctuation">}</span>
style<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>backButtonEnabled <span class="token operator">?</span> styles<span class="token punctuation">.</span>navButton <span class="token punctuation">:</span> styles<span class="token punctuation">.</span>disabledButton<span class="token punctuation">}</span><span class="token operator">></span>
<Text<span class="token operator">></span>
<span class="token punctuation">{</span><span class="token string">'<'</span><span class="token punctuation">}</span>
<<span class="token operator">/</span>Text<span class="token operator">></span>
<<span class="token operator">/</span>TouchableOpacity<span class="token operator">></span>
<TouchableOpacity
onPress<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>goForward<span class="token punctuation">}</span>
style<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>forwardButtonEnabled <span class="token operator">?</span> styles<span class="token punctuation">.</span>navButton <span class="token punctuation">:</span> styles<span class="token punctuation">.</span>disabledButton<span class="token punctuation">}</span><span class="token operator">></span>
<Text<span class="token operator">></span>
<span class="token punctuation">{</span><span class="token string">'>'</span><span class="token punctuation">}</span>
<<span class="token operator">/</span>Text<span class="token operator">></span>
<<span class="token operator">/</span>TouchableOpacity<span class="token operator">></span>
<TextInput
ref<span class="token operator">=</span><span class="token punctuation">{</span>TEXT_INPUT_REF<span class="token punctuation">}</span>
autoCapitalize<span class="token operator">=</span><span class="token string">"none"</span>
defaultValue<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>url<span class="token punctuation">}</span>
onSubmitEditing<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>onSubmitEditing<span class="token punctuation">}</span>
onChange<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>handleTextInputChange<span class="token punctuation">}</span>
clearButtonMode<span class="token operator">=</span><span class="token string">"while-editing"</span>
style<span class="token operator">=</span><span class="token punctuation">{</span>styles<span class="token punctuation">.</span>addressBarTextInput<span class="token punctuation">}</span>
<span class="token operator">/</span><span class="token operator">></span>
<TouchableOpacity onPress<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>pressGoButton<span class="token punctuation">}</span><span class="token operator">></span>
<View style<span class="token operator">=</span><span class="token punctuation">{</span>styles<span class="token punctuation">.</span>goButton<span class="token punctuation">}</span><span class="token operator">></span>
<Text<span class="token operator">></span>
Go<span class="token operator">!</span>
<<span class="token operator">/</span>Text<span class="token operator">></span>
<<span class="token operator">/</span>View<span class="token operator">></span>
<<span class="token operator">/</span>TouchableOpacity<span class="token operator">></span>
<<span class="token operator">/</span>View<span class="token operator">></span>
<WebView
ref<span class="token operator">=</span><span class="token punctuation">{</span>WEBVIEW_REF<span class="token punctuation">}</span>
automaticallyAdjustContentInsets<span class="token operator">=</span><span class="token punctuation">{</span><span class="token boolean">false</span><span class="token punctuation">}</span>
style<span class="token operator">=</span><span class="token punctuation">{</span>styles<span class="token punctuation">.</span>webView<span class="token punctuation">}</span>
source<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span>uri<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>url<span class="token punctuation">}</span><span class="token punctuation">}</span>
javaScriptEnabled<span class="token operator">=</span><span class="token punctuation">{</span><span class="token boolean">true</span><span class="token punctuation">}</span>
domStorageEnabled<span class="token operator">=</span><span class="token punctuation">{</span><span class="token boolean">true</span><span class="token punctuation">}</span>
decelerationRate<span class="token operator">=</span><span class="token string">"normal"</span>
onNavigationStateChange<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>onNavigationStateChange<span class="token punctuation">}</span>
onShouldStartLoadWithRequest<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>onShouldStartLoadWithRequest<span class="token punctuation">}</span>
startInLoadingState<span class="token operator">=</span><span class="token punctuation">{</span><span class="token boolean">true</span><span class="token punctuation">}</span>
scalesPageToFit<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>scalesPageToFit<span class="token punctuation">}</span>
<span class="token operator">/</span><span class="token operator">></span>
<View style<span class="token operator">=</span><span class="token punctuation">{</span>styles<span class="token punctuation">.</span>statusBar<span class="token punctuation">}</span><span class="token operator">></span>
<Text style<span class="token operator">=</span><span class="token punctuation">{</span>styles<span class="token punctuation">.</span>statusBarText<span class="token punctuation">}</span><span class="token operator">></span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>status<span class="token punctuation">}</span><<span class="token operator">/</span>Text<span class="token operator">></span>
<<span class="token operator">/</span>View<span class="token operator">></span>
<<span class="token operator">/</span>View<span class="token operator">></span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
},
goBack: function() { this.refs[WEBVIEW_REF].goBack(); },
goForward: function() { this.refs[WEBVIEW_REF].goForward(); },
reload: function() { this.refs[WEBVIEW_REF].reload(); },
onShouldStartLoadWithRequest: function(event) { // Implement any custom loading logic here, don't forget to return! return true; },
onNavigationStateChange: function(navState) { this.setState({ backButtonEnabled: navState.canGoBack, forwardButtonEnabled: navState.canGoForward, url: navState.url, status: navState.title, loading: navState.loading, scalesPageToFit: true }); },
onSubmitEditing: function(event) { this.pressGoButton(); },
pressGoButton: function() { var url = this.inputText.toLowerCase(); if (url === this.state.url) { this.reload(); } else { this.setState({ url: url, }); } // dismiss keyboard this.refs[TEXT_INPUT_REF].blur(); },
});
var Button = React.createClass({ _handlePress: function() { if (this.props.enabled !== false && this.props.onPress) { this.props.onPress(); } }, render: function() { return ( <TouchableWithoutFeedback onPress={this._handlePress}> <View style={[styles.button, this.props.enabled ? {} : styles.buttonDisabled]}> <Text style={styles.buttonText}>{this.props.text}</Text> </View> </TouchableWithoutFeedback> ); } });
var ScaledWebView = React.createClass({
getInitialState: function() { return { scalingEnabled: true, } },
render: function() { return ( <View> <WebView style={{ backgroundColor: BGWASH, height: 200, }} source={{uri: 'https://facebook.github.io/react/'}} scalesPageToFit={this.state.scalingEnabled} /> <View style={styles.buttons}> { this.state.scalingEnabled ? <Button text="Scaling:ON" enabled={true} onPress={() => this.setState({scalingEnabled: false})} /> : <Button text="Scaling:OFF" enabled={true} onPress={() => this.setState({scalingEnabled: true})} /> } </View> </View> ); }, })
var styles = StyleSheet.create({ container: { flex: 1, backgroundColor: HEADER, }, addressBarRow: { flexDirection: 'row', padding: 8, }, webView: { backgroundColor: BGWASH, height: 350, }, addressBarTextInput: { backgroundColor: BGWASH, borderColor: 'transparent', borderRadius: 3, borderWidth: 1, height: 24, paddingLeft: 10, paddingTop: 3, paddingBottom: 3, flex: 1, fontSize: 14, }, navButton: { width: 20, padding: 3, marginRight: 3, alignItems: 'center', justifyContent: 'center', backgroundColor: BGWASH, borderColor: 'transparent', borderRadius: 3, }, disabledButton: { width: 20, padding: 3, marginRight: 3, alignItems: 'center', justifyContent: 'center', backgroundColor: DISABLED_WASH, borderColor: 'transparent', borderRadius: 3, }, goButton: { height: 24, padding: 3, marginLeft: 8, alignItems: 'center', backgroundColor: BGWASH, borderColor: 'transparent', borderRadius: 3, alignSelf: 'stretch', }, statusBar: { flexDirection: 'row', alignItems: 'center', paddingLeft: 5, height: 22, }, statusBarText: { color: 'white', fontSize: 13, }, spinner: { width: 20, marginRight: 6, }, buttons: { flexDirection: 'row', height: 30, backgroundColor: 'black', alignItems: 'center', justifyContent: 'space-between', }, button: { flex: 0.5, width: 0, margin: 5, borderColor: 'gray', borderWidth: 1, backgroundColor: 'gray', }, });
const HTML = <<span class="token operator">!</span>DOCTYPE html<span class="token operator">></span>\n <html<span class="token operator">></span> <head<span class="token operator">></span> <title<span class="token operator">></span>Hello Static World<<span class="token operator">/</span>title<span class="token operator">></span> <meta http<span class="token operator">-</span>equiv<span class="token operator">=</span><span class="token string">"content-type"</span> content<span class="token operator">=</span><span class="token string">"text/html; charset=utf-8"</span><span class="token operator">></span> <meta name<span class="token operator">=</span><span class="token string">"viewport"</span> content<span class="token operator">=</span><span class="token string">"width=320, user-scalable=no"</span><span class="token operator">></span> <style type<span class="token operator">=</span><span class="token string">"text/css"</span><span class="token operator">></span> body <span class="token punctuation">{</span> margin<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">;</span> padding<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">;</span> font<span class="token punctuation">:</span> <span class="token number">62.5</span><span class="token operator">%</span> arial<span class="token punctuation">,</span> sans<span class="token operator">-</span>serif<span class="token punctuation">;</span> background<span class="token punctuation">:</span> #ccc<span class="token punctuation">;</span> <span class="token punctuation">}</span> h1 <span class="token punctuation">{</span> padding<span class="token punctuation">:</span> 45px<span class="token punctuation">;</span> margin<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">;</span> text<span class="token operator">-</span>align<span class="token punctuation">:</span> center<span class="token punctuation">;</span> color<span class="token punctuation">:</span> #33f<span class="token punctuation">;</span> <span class="token punctuation">}</span> <<span class="token operator">/</span>style<span class="token operator">></span> <<span class="token operator">/</span>head<span class="token operator">></span> <body<span class="token operator">></span> <h1<span class="token operator">></span>Hello Static World<<span class="token operator">/</span>h1<span class="token operator">></span> <<span class="token operator">/</span>body<span class="token operator">></span> <<span class="token operator">/</span>html<span class="token operator">></span>;
exports.displayName = (undefined: ?string); exports.title = '<WebView>'; exports.description = 'Base component to display web content'; exports.examples = [ { title: 'Simple Browser', render(): ReactElement<any> { return <WebViewExample />; } }, { title: 'Scale Page to Fit', render(): ReactElement<any> { return <ScaledWebView/>; } }, { title: 'Bundled HTML', render(): ReactElement<any> { return ( <WebView style={{ backgroundColor: BGWASH, height: 100, }} source={require('./helloworld.html')} scalesPageToFit={true} /> ); } }, { title: 'Static HTML', render(): ReactElement<any> { return ( <WebView style={{ backgroundColor: BGWASH, height: 100, }} source={{html: HTML}} scalesPageToFit={true} /> ); } }, { title: 'POST Test', render(): ReactElement<any> { return ( <WebView style={{ backgroundColor: BGWASH, height: 100, }} source={{ uri: 'http://www.posttestserver.com/post.php', method: 'POST', body: 'foo=bar&bar=foo' }} scalesPageToFit={false} /> ); } } ];

