React本机深层链接在iOS上不起作用

时间:2019-07-19 11:37:14

标签: react-native deep-linking react-native-ios react-native-router-flux

我已将com.testApp://challenge/:id设置为深层链接URL。如果应用已关闭,并且我尝试通过浏览器作为URL来运行,则效果很好。另外,这仅适用于iOS。

但是如果打开了应用程序并且我尝试从浏览器访问上述URL,则失败。

我已使用this article作为参考。尽管它有if (Platform.OS === 'android')个条件,但没有帮助。

以下是我的代码:

  componentDidMount() {
    Linking.addEventListener('url', this.handleOpenURL);
    Linking.getInitialURL().then(url => {
      this.navigate(url);
    });
  }

  componentWillUnmount() {
    Linking.removeEventListener('url', this.handleOpenURL);
  }

  handleOpenURL = (event) => {
    if(event){
      this.navigate(event.url);
    }
  }

  navigate = (url) => { // E
    if (url){
      const route = url.replace(/.*?:\/\//g, '');
      const id = route.match(/\/([^\/]+)\/?$/)[1];
      const routeName = route.split('/')[0];

      if (routeName === 'challenge') {
        ChallengeService.find(id).then((challenge) => {
          Actions.challenge({ challenge: challenge });
        });
      };
    }
  }

以下是我的AppDelegate.m代码

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  return [RCTLinkingManager application:application openURL:url
                      sourceApplication:sourceApplication annotation:annotation];
}

让我知道我在做什么错。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并以此解决了问题

<WebView
    source={{ uri }}
    onShouldStartLoadWithRequest={request => {
    console.log('request :', request);
    return request.url.startsWith(request.url);
    }}
/>
相关问题