防止在React Native WebView中重新加载

时间:2019-01-18 23:03:10

标签: reactjs react-native webview

我正在React Native应用程序内的WebView组件内运行一个单独的Web应用程序,并试图使它们正确通信。

对WebView进行本机反应可以正常工作。我可以打电话给webView.postMessage(...),并在document.addEventListener("message", ...)接收它,没有任何问题。

但是,当我尝试采用另一种方法(从WebView到Native)时,对window.postMessage triggers a url change via window.location的调用似乎重新加载了整个WebView,并中断了其中的路由解决方案。

react-native-community/react-native-webview组件似乎have the same problem

有什么方法可以在不更改URL或不重新加载页面的情况下从Web视图内向本机应用发送消息?

1 个答案:

答案 0 :(得分:0)

你可以为 IOS 使用 onShouldStartLoadWithRequest 道具:-

import React, {Component, useCallback} from 'react';
import {
  BackHandler,
  Platform,
  SafeAreaView,
  ActivityIndicator,
  StyleSheet,
  Dimensions,
  Linking,
} from 'react-native';
import {WebView} from 'react-native-webview';
import Spinner from 'react-native-loading-spinner-overlay';


class SupportPayWebView extends Component {
  constructor(props) {
    super(props);
    
  }
  webView = {
    canGoBack: false,
    ref: null,
  };

  loader = {
    show: true,
  };

  onAndroidBackPress = () => {
    if (this.webView.canGoBack && this.webView.ref) {
      this.webView.ref.goBack();
      return true;
    }
    return false;
  };

  componentWillMount() {
    
    if (Platform.OS === 'android') {
      BackHandler.addEventListener(
        'hardwareBackPress',
        this.onAndroidBackPress,
      );
    } else {
      BackHandler.addEventListener('hardwareBackPress', this.backHandler);
    }
  }

  componentWillUnmount() {
    
    if (Platform.OS === 'android') {
      BackHandler.removeEventListener('hardwareBackPress');
    } else {
      BackHandler.removeEventListener('hardwareBackPress', this.backHandler);
    }
  }

  backHandler = () => {
   
    this.webView.ref.goBack();
    return true;
  
  };

  ActivityIndicatorLoadingView() {
    return (
      <ActivityIndicator
        color="#009688"
        size="large"
        style={styles.ActivityIndicatorStyle}
      />
    );
  }
  
  onShouldStartLoadWithRequest = (navigator) => {
    
      this.webView.ref.stopLoading();
      return false;

  };
  render() {
    return (
      <>
        <WebView
          style={styles.WebViewStyle}
          onLoadEnd={() => {
            this.loader.show = false;
          }}
          injectedJavaScript={
            "const meta = document.createElement('meta'); meta.setAttribute('content', 'width=device-width, initial-scale=0.5, maximum-scale=0.5, user-scalable=0'); meta.setAttribute('name', 'viewport'); document.getElementsByTagName('head')[0].appendChild(meta); "
          }
          
          scalesPageToFit={true}
          automaticallyAdjustContentInsets={true}
          scrollEnabled={true}
          javaScriptEnabled={true}
          domStorageEnabled={true}
          renderLoading={this.ActivityIndicatorLoadingView}
          startInLoadingState={true}
          bounces={false}
          source={{uri: '}}
          ref={(webView) => {
            this.webView.ref = webView;
          }}
          onShouldStartLoadWithRequest={this.onShouldStartLoadWithRequest}
          
          sharedCookiesEnabled={true}
          // scalesPageToFit={false}
          javaScriptEnabledAndroid={true}
          userAgent="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
          // decelerationRate="normal"
        />
       


      </>
      // </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  WebViewStyle: {
    justifyContent: 'center',
    alignItems: 'center',

    marginTop: Platform.OS === 'ios' ? 35 : 0,
    width: '100%',
    height: '100%',
    resizeMode: 'cover',
    flex: 1,
  },

  ActivityIndicatorStyle: {
    position: 'absolute',
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    alignItems: 'center',
    justifyContent: 'center',
  },
});