从WebView内部反应本机深层链接

时间:2019-01-18 05:55:25

标签: ios react-native webview deep-linking

我正在使用带有React Navigationreact-native-deep-linkingreact-native-webview软件包的React Native。

我的应用程序正在使用深层链接访问应用程序的不同屏幕,并且可以从iPhone上的深层链接中正常工作,除非从WebView内打开(如果我按该链接则什么也没有发生)。 该应用程序甚至对单击链接,没有console.warn消息等都没有反应。

如果我改用iPhone上的Safari,则功能可以正常运行,但不能在WebView内运行。

这是WebView代码:

class BankID extends React.Component {
  render() {
      return (
        <WebView
          style={{ flex: 1 }}
          source={{ uri: 'https://URL/file.html' }}
        />
      );
  }
}
export default BankID;

file.html:

<html>
<body>
<a href="test://1234">App-Link</a>
</body>
</html>

从App.js中,我得到了github repo中指示的深层链接组件:

componentDidMount() {
  DeepLinking.addScheme('test://');

  Linking.addEventListener('url', this.handleUrl);

    Linking.getInitialURL().then((url) => {
      if (url) {
        Linking.openURL(url);
      }
    }).catch(err => console.error('An error occurred', err));

    DeepLinking.addRoute('/:id', ({ id }) => {
      this.setState({ roomKey: id.toString() });
      if (this.vidyoConnector) {
        this.callButtonPressHandler();
      }
    });
 }
  handleUrl = ({ url }) => {
    Linking.canOpenURL(url).then((supported) => {
      if (supported) {
        DeepLinking.evaluateUrl(url);
      }
    }).catch((error) => {
      console.warn('handleUrl failed with the following reason: ', error);
    });
  }

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:0)

这对我有用:

在Web视图中,我在onShouldStartLoadWithRequest中添加了一个函数。

$n

然后是功能:

#!/usr/bin/perl

use strict;
use warnings;
use v5.14;


my @arr = ( [qw(0 1 2 3 4 5)],
            [qw(a b c d e f)],
            [qw(5 4 3 2 1 0)],
            [qw(9 8 7 6 5 4)],
            [qw(f e d c b a)],
            [qw(4 5 6 7 8 9)] );

print_matrix(@arr);

rotate_matrix(\@arr, 2, 2);
say "";

print_matrix(@arr);


sub rotate_matrix {
    my ($matrix, $target_layer, $n) = @_;
    --$target_layer; # I prefer a 0-indexed value
    # TODO: check that $target_layer < @$matrix/2
    # TODO: do something like $n = $n % (num of elements in layer)

    my @queue;
    my ($i, $j) = ($target_layer, $target_layer);
    my ($max_i, $max_j) = (@{$matrix->[0]}-$target_layer-1, @$matrix-$target_layer-1);

    while (1) { # Actually a do...while loop (see 'continue' block)
        push @queue, $matrix->[$i][$j];
        next unless @queue > $n; # Waiting to reach n-th element
        $matrix->[$i][$j] = shift @queue;
    } continue {
        ($i, $j) = next_ij($target_layer,$max_i,$max_j,$i,$j);
        # Stopping if we are back at the begining
        last if $i == $target_layer && $j == $target_layer;
    }

    # Emptying queue
    while (@queue) {
        $matrix->[$i][$j] = shift @queue;
        ($i, $j) = next_ij($target_layer,$max_i,$max_j,$i,$j);
    }


}

# Computes the index of the next element of the layer
sub next_ij {
    my ($target_layer, $max_i, $max_j, $i, $j) = @_;
    if ($j == $max_j) { # Last column
        if ($i == $max_i) { # Last row (bottom right -> going left)
            return ($i, $j-1);
        } else { # Not last row (somewhere on the right col -> going down)
            return ($i+1, $j);
        }
    } elsif ($j == $target_layer) { # First column
        if ($i == $target_layer) { # First row (top left -> going right)
            return ($i, $j+1);
        } else { # Not top row (somewhere on the left col -> going up)
            return ($i-1, $j);
        }
    } else { # Neither first nor last column
        if ($i == $target_layer) { # First row (somewhere on the top row -> going right)
            return ($i, $j+1);
        } else { # Last row (somewhere on the last row -> going left)
            return ($i, $j-1);
        }
    }
}



# Prints given matrix
sub print_matrix {
    foreach my $row (@_) {
        my $cnt=0;
        foreach my $char (@$row) {
            print $char; $cnt++;
            if ( $cnt == scalar(@_) ) {
                print "\n";
            } else {
                print " ";
            }
        }
    }
}

无需在App.js中进行任何更改

答案 1 :(得分:0)

我遇到onNavigationStateChange的问题,打开外部网址时留下了不可篡改/可点击的页面,所以我使用了它,现在效果很好。

        <WebView
          ref={webView}
          source={{ uri: uri }}
          startInLoadingState // ignore warning
          onMessage={handleOnMessage}
          javaScriptEnabled
          onShouldStartLoadWithRequest={handleOnShouldStartLoadWithRequest}
        />
  const handleOnShouldStartLoadWithRequest = (event) => {
    const hostname = extractHostname(event.url)

    if (event.url && ![BASE_DOMAIN, APPLEID_DOMAIN].includes(hostname)) {
      Linking.openURL(event.url)
      return false
    } else {
      return true
    }
  }