React Native iOS:如何在应用程序关闭时收听传入的链接并进行处理?

时间:2018-11-04 20:02:09

标签: javascript ios react-native event-handling deep-linking

我正在尝试实施已阅读到我的App中的教程。我已经建立了一种侦听传入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方法。我不确定是否错过了一步。另一种可能性是也许我必须在其他地方添加事件侦听器。不知道该怎么办。

2 个答案:

答案 0 :(得分:2)

使用此解决了

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

答案 1 :(得分:1)

您需要在Xcode中指定url方案。转到项目设置,在“信息”下应指向“ URL类型”,然后在此处定义链接。 more about deep linking