在 Android 上使用 react-native-webview 时如何防止重定向

How to prevent redirecting when using react-native-webview on Android

我想阻止重定向到 react-native-webview 中的新页面。 我不想重定向,而是想打开新的 webview 模式并在那里打开 url。

有没有办法做到这一点?

在 IOS 中,使用 stopLoading 工作正常,但在 Android、

上不起作用
onNavigationStateChanged(navState) {
    if(navState.canGoBack)
    {
        this._webView.stopLoading();

        if(navState.url.indexOf('newWebViewPage') !== -1)
        {
            this.props.navigation.navigate('WebViewModal',{
                'url':navState.url,
                'title':navState.url
            })

            return false;
        }
        else
        {
            return true;
        }
    }
}

render() {
    let headers = {
        'Authorization':'Basic ' + btoa(NetworkUtils.USERNAME + ":" + NetworkUtils.PASSWORD),
        'Content-Type':'multipart/form-data',
    }

    return (
        <WebView
            source={{ uri: this.props.url, headers:headers }}
            bounces={false}
            javaScriptEnabled={true}
            onMessage={this.onMessage.bind(this)}
            injectedJavaScript={injectedJavascript}
            ref={(webView) => { this._webView = webView; }}
            style={[styles.webview,this.props.style]}
            startInLoadingState={false}
            useWebKit={true}
            onNavigationStateChange={this.onNavigationStateChanged.bind(this)}
            onLoadEnd={this.props.onLoadEnd?this.props.onLoadEnd:()=>{}}
            mixedContentMode={'compatibility'}
        />
    );
}

请帮忙!!

我正在使用 React 导航。我这样做:

navigationRedirect = navState => {
    const { dispatch } = this.props.navigation
    const url = navState.url

      if(url.includes('www.unotv.com')){

         if(navState.canGoBack && navState.loading){

            this.webview.goBack()

             return dispatch(StackActions.push({
                 routeName: 'VisorWebView',
                 params: { url },
                 actions: [this.trackingForAnalytics(url)] //This function not affect the navigation
            }))

         }

     }else{
           this.webview.stopLoading()
           this.webview.goBack()
           Linking.openURL(url)
    }

}

  <WebView
     ref={ref => this.webview = ref }
     source={{ uri: this.state.baseURI }}
     originWhitelist={["https"]}
     userAgent="Mozilla/5.0 (Linux; Android 4.4.4; One Build/KTU84L.H4) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/28.0.0.20.16;]"
                decelerationRate='normal'
                allowsInlineMediaPlayback={true}                    
                mediaPlaybackRequiresUserAction={false}
                sharedCookiesEnabled={true}
                mixedContentMode='always'
                startInLoadingState={true}
                containerStyle={{ marginTop: isTablet ? -60 : -50 }}
                renderLoading={() => (
                    <ActivityIndicator
                        color='#29aae1'
                        size='large'
                        style={{ flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'flex-start' }}
                    />
                )}
                onNavigationStateChange={this.redirect}
            />

当触摸 link 时,我的 domine 会打开浏览器 (Safari)。如果 link 在我的域中,请使用其他 webview 打开另一个屏幕 :)