Renders a native WebView.
Props
View props...
automaticallyAdjustContentInsets bool
contentInset {top: number, left: number, bottom: number, right: number}
html string

Use the source
prop instead.
injectedJavaScript string
Sets the JS to be injected when the webpage loads.
mediaPlaybackRequiresUserAction bool
Determines whether HTML5 audio & videos require the user to tap before they can start playing. The default value is false
.
onError function
Invoked when load fails
onLoad function
Invoked when load finish
onLoadEnd function
Invoked when load either succeeds or fails
onLoadStart function
Invoked on load start
onNavigationStateChange function
renderError function
Function that returns a view to show if there's an error.
renderLoading function
Function that returns a loading indicator.
scalesPageToFit bool
Sets whether the webpage scales to fit the view and the user can change the scale.
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
style View#style
url string

Use the source
prop instead.
androiddomStorageEnabled bool
Used on Android only, controls whether DOM Storage is enabled or not
androidjavaScriptEnabled bool
Used on Android only, JS is enabled by default for WebView on iOS
iosallowsInlineMediaPlayback bool
Determines whether HTML5 videos play inline or use the native full-screen controller. default value 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
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 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 WebView)
iosonShouldStartLoadWithRequest function
Allows custom handling of any webview requests by a JS handler. Return true or false from this method to continue loading the request.
iosscrollEnabled bool
Methods
goForward()
Go forward one page in the webview's history.
goBack()
Go back one page in the webview's history.
reload()
Reloads the current page.
stopLoading()
getWebViewHandle(): any
Returns the native webview node.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | 'use strict' ; 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 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)) { } this .inputText = url; }, render: function () { this .inputText = this .state.url; return ( <View style={[styles.container]}> <View style={[styles.addressBarRow]}> <TouchableOpacity onPress={ this .goBack} style={ this .state.backButtonEnabled ? styles.navButton : styles.disabledButton}> <Text> { '<' } </Text> </TouchableOpacity> <TouchableOpacity onPress={ this .goForward} style={ this .state.forwardButtonEnabled ? styles.navButton : styles.disabledButton}> <Text> { '>' } </Text> </TouchableOpacity> <TextInput ref={TEXT_INPUT_REF} autoCapitalize= "none" defaultValue={ this .state.url} onSubmitEditing={ this .onSubmitEditing} onChange={ this .handleTextInputChange} clearButtonMode= "while-editing" style={styles.addressBarTextInput} /> <TouchableOpacity onPress={ this .pressGoButton}> <View style={styles.goButton}> <Text> Go! </Text> </View> </TouchableOpacity> </View> <WebView ref={WEBVIEW_REF} automaticallyAdjustContentInsets={ false } style={styles.webView} source={{uri: this .state.url}} javaScriptEnabled={ true } domStorageEnabled={ true } decelerationRate= "normal" onNavigationStateChange={ this .onNavigationStateChange} onShouldStartLoadWithRequest={ this .onShouldStartLoadWithRequest} startInLoadingState={ true } scalesPageToFit={ this .state.scalesPageToFit} /> <View style={styles.statusBar}> <Text style={styles.statusBarText}>{ this .state.status}</Text> </View> </View> ); }, 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 = ` <!DOCTYPE html>\n <html> <head> <title>Hello Static World</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=320, user-scalable=no"> <style type="text/css"> body { margin: 0; padding: 0; font: 62.5% arial, sans-serif; background: #ccc; } h1 { padding: 45px; margin: 0; text-align: center; color: #33f; } </style> </head> <body> <h1>Hello Static World</h1> </body> </html> `; 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 } /> ); } } ]; |