RN provides us with the WebView component to load a web page.
/**
* Created by gyg on 2017/5/4.
*/
'use strict'
import React, {Component} from 'react';
import {
StyleSheet,
View,
Text,
WebView,
BackAndroid,
} from 'react-native';
class WebLoadingView extends Component {
render() {
return (
<View style={{flex:1,justifyContent:'center',
alignItems:'center',backgroundColor:'#f2f2f2'}}>
<Text style={styles.loadingText}>
The page is loading...
</Text>
</View>
)
}
}
export default class WebViewScene extends Component {
// structure
constructor(props) {
super(props);
// initial state
this.state = {
url: "",
title: "",
loading: true,
isBackButtonEnable: false,
isForwardButtonEnable: false
};
}
componentDidMount() {
BackAndroid.addEventListener("webHardwareBackPress", ()=> {
try {
if (this.state.isBackButtonEnable) {
this.refs._webView.goBack();//Return to the previous page
return true;//true system will no longer process false to the system
}
} catch (error) {
return false;
}
return false;
})
}
componentWillUnmount() {
BackAndroid.removeEventListener("webHardwareBackPress");
}
render() {
return (
<View style={styles.container}>
<WebView
style={styles.webView}
ref="_webView"
source={{uri:this.props.route.extras.url}}//Get URL parameters
automaticallyAdjustContentInsets={true}
domStorageEnabled={true}
javaScriptEnabled={true}
scalesPageToFit={true}
startInLoadingState={true}
renderLoading={()=>{return <WebLoadingView/>}}
onNavigationStateChange={this._onNavigationStateChange.bind(this)}
/>
</View>
)
}
//WebView navigation status changes
_onNavigationStateChange(navState) {
this.setState({
url: navState.url,
title: navState.title,
loading: navState.loading,
isBackButtonEnable: navState.canGoBack,
isForwardButtonEnable: navState.canGoForward,
})
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#f2f2f2",
},
webview: {
flex: 1,
},
loadingText: {
color: '#8a8a8a',
fontSize: 16
}
})
this.props.navigator.push({
component: WebViewScene,
extras: {
url: 'https://www.jd.com',//Web URL
}
})
Solution injectedJavascript should instead be the following (function(){ var originalPostMessage = window.postMessage; var patchedPostMessage = function(message,targetOrigin,transfer){&n...
Mobile terminal such as Android webview is based on the height of his own distraction, but RN has not, so how to do it? 2 one way easiest: usingreact-native-autoheight-webview How to use: Then use: Th...
rn encapsulates native jsbridge to interact with H5 rn webview problem I wrote an article that encapsulates native webview before,rn encapsulates native jsbridge to interact with H5 But that’s o...
Preface When developing Android, we generally have some requirements to load web pages or execute some JavaScript. We all know that the control that implements this function in Android is WebView. The...
creates a native WebView that can be used to visit a web page. Screenshot Attributes iosallowsInlineMediaPlayback bool # Specify whether the HTML5 video is played at the current position of the w...
problem Opening a WebView in React Native will have a very obvious white screen time. analyse problem I read some optimizations about WebView on the Internet, refer to the following WebView ope...
1. Inject JavaScript into WebView When using injectJavaScript to inject javascript in webview, the method is as follows When injecting javascript strings through injectedJavaScript, only static text c...
Like Android and IOS, there is also a control WebView that loads webpage data in React Native. Common methods are: source {uri: string, method: string, headers: object, body: string}, {html: string, b...
In React Native, WebViews can allow mobile apps to access any web portal. In other words, the web view allows us to open a web URL in the app interface. Although React Native provides a built-in web v...
H5 sends data Window.reactnativeWebView.PostMessage ('event type type'); RN receives data (This.WebView is a react-native-webview instance by refs) Way, this.webview.injectjavascript ('H5 function nam...