React Native - open links in browser

前端 未结 13 661
遇见更好的自我
遇见更好的自我 2020-12-04 11:59

Hi i am using react native\'s webview to display some html, i want that whenever a user clicks a link inside that html, it will open the user\'s browser with that link.

相关标签:
13条回答
  • 2020-12-04 12:41

    Here is a complete working solution:

    import React, { Component } from 'react';
    import { WebView, Linking } from 'react-native';
    
    export default class WebViewThatOpensLinksInNavigator extends Component {
      render() {
        const uri = 'http://stackoverflow.com/questions/35531679/react-native-open-links-in-browser';
        return (
          <WebView
            ref={(ref) => { this.webview = ref; }}
            source={{ uri }}
            onNavigationStateChange={(event) => {
              if (event.url !== uri) {
                this.webview.stopLoading();
                Linking.openURL(event.url);
              }
            }}
          />
        );
      }
    }
    

    It uses a simple WebView, intercepts any url change, and if that url differs from the original one, stops the loading, preventing page change, and opens it in the OS Navigator instead.

    0 讨论(0)
  • 2020-12-04 12:42

    I had many issues with this solution while using local HTML file only at iOS. For instance, when I had Instagram embed in my webview it opened it automatically. In order to solve it I've added (only for iOS):

    onShouldStartLoadWithRequest={event => {
        const condition = isIos ? event.navigationType == 'click' : true
        if (event.url.slice(0, 4) === 'http' && condition) {
          Linking.openURL(event.url)
          return false
        }
      return true
    }}
    
    0 讨论(0)
  • 2020-12-04 12:43

    In addition to the excellent answer https://stackoverflow.com/a/40382325/10236907: When using source={{html: '...'}}, you can check for an external url change using: if (!/^data:text/.test(event.url)) {

    0 讨论(0)
  • 2020-12-04 12:44

    If stopLoading() in onNavigationStateChange on Android does not work,

    this.webview.stopLoading();
    setTimeOut( () => {
      Linking.openURL(event.url)
    }, 1000);
    

    It worked well.

    0 讨论(0)
  • 2020-12-04 12:46
    1. If you are attempting to open a redirect url and you get an error from an android intent such as "err_unknown_url_scheme".

    2. Check what it is trying to open because it may have detected android traffic and attempted to open in the corresponding app.

    3. If you wish for it to open in the webview anyway and you get an intent back from a redirect, intercept it in onNavigationStateChange and rewrite it:

    onNavigationStateChange={event => {
      if (event.url !== uri) {
          if (event.url.match('intent://')) {
              SetSearchClick(
                  event.url.replace('intent://', 'https://'),
              );
             }
       }
    }}
    
    
    0 讨论(0)
  • 2020-12-04 12:46
    <WebView
          source={{uri: this.state.src}}
          ref={(webView) => {
            this.webView.ref = webView;
          }}
          onNavigationStateChange={(navState) => {
            this.webView.canGoBack = navState.canGoBack;
            if (!navState.url.includes('yourdomain.com')) {
              Linking.openURL(navState.url);
              return false;
            }
          }}
          onShouldStartLoadWithRequest={(event) => {
            if (!event.url.includes('yourdomain.com')) {
              Linking.openURL(event.url);
              return false;
            }
            return true;
          }}
          style={{flex: 1}}
          startInLoadingState={true}
          renderLoading={() => {
            return (
              <View style={[styles.container, styles.horizontal]}>
                <ActivityIndicator size="large" color="#0000ff" />
              </View>
            );
          }}
        />
    

    I struggled with this one, my use case was to open external links in a separate browser. This solution worked for me.

    0 讨论(0)
提交回复
热议问题