从 WebView 中反应原生深层链接

Sim*_*onB 5 deep-linking webview ios react-native

我正在使用 React Native 和React Navigation以及react-native-deep-linkingreact-native-webview包。

我的应用程序正在使用深层链接来访问应用程序的不同屏幕,并且在 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;
Run Code Online (Sandbox Code Playgroud)

文件.html:

<html>
<body>
<a href="test://1234">App-Link</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

从 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);
    });
  }
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激。谢谢你。

Sim*_*onB 5

这对我有用:

在 webview 中,我添加了一个函数 onShouldStartLoadWithRequest。

  <WebView
    other
    stuff
    onShouldStartLoadWithRequest={this.openExternalLink}
  />
Run Code Online (Sandbox Code Playgroud)

然后是函数:

  openExternalLink= (req) => {
    const isHTTPS = req.url.search('https://') !== -1;

        if (isHTTPS) {
          return true;
        } else {
          if (req.url.startsWith("test://")) {
            this.props.navigation.navigate('Home');
          } 
          return false;
        }
      }
Run Code Online (Sandbox Code Playgroud)

无需更改 App.js 中的任何内容

  • 当用户单击 WebView 内的 [deep]-link 调用时,这不起作用 (2认同)