React Native iOS:如何监听传入链接并在应用关闭时处理它们?

React Native iOS: How to listen to incoming links and handle them when the app is closed?

我正在尝试将我读过的教程应用到我的应用程序中。我已经设置了一种方法来监听传入的 url 并处理它们。

componentDidMount() {
  Linking.addEventListener('url', this.handleOpenURL);
}
componentWillUnmount() {
  Linking.removeEventListener('url', this.handleOpenURL);
}
handleOpenURL(event) {
  console.log(event.url);
  const route = e.url.replace(/.*?:\/\//g, '');
  // do something with the url, in our case navigate(route)
}

如果应用程序在后台运行,此方法效果很好。但是,我 运行 遇到的问题是当应用程序关闭时, handleOpenURL 方法没有被调用。我不确定我是否错过了一步。另一种可能性是我可能必须在其他地方添加事件监听器。不知道该怎么做。

用这个解决了

 Linking.getInitialURL().then((url) => {
    if (url) {
      AppListeners.handleOpenUrl({ url });
    }
  });

您需要在 Xcode 中指定 url-方案。转到您的项目设置,在 "info" 下应该有一个点 "Url types",然后在那里定义您的 link。 more about deep linking