用于深层链接后未清除linking.getInitialURL() [英] Linking.getInitialURL() is not being cleared after used for deeplink

查看:560
本文介绍了用于深层链接后未清除linking.getInitialURL()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题我已经有两个星期了.我使用 Wix的导航来浏览应用程序.我遵循了本教程来实现Deeplink/Universal链接. >

我有一个名为BaseScreen的基类,其中像教程中一样保留了所有的Deeplink处理程序. BaseScreen看起来像这样:

componentDidMount(){
    // this handles the case where the app is closed and is launched via Universal Linking.
    Linking.getInitialURL()
        .then((url) => {
          if (url) {
            // Alert.alert('GET INIT URL','initial url  ' + url)
            this.resetStackToProperRoute(url)
          }
        })
        .catch((e) => {})

   // This listener handles the case where the app is woken up from the Universal or Deep Linking
   Linking.addEventListener('url', this.appWokeUp);
  }

  componentWillUnmount(){
    // Remove the listener
    Linking.removeEventListener('url', this.appWokeUp);
  }

  appWokeUp = (event) => {
    // this handles the use case where the app is running in the background and is activated by the listener...
    // Alert.alert('Linking Listener','url  ' + event.url)
    this.resetStackToProperRoute(event.url)
  }

  resetStackToProperRoute = (url) => {
    // grab the trailing portion of the url so we can use that data to fetch proper information from the server
    let trailing = url.slice(url.lastIndexOf('=') + 1, url.length)
    // go to the desired screen with the trailing token grabbed from the url
    this.props.navigator.resetTo({
      screen: 'NewPassword',
      overrideBackPress: true,
      passProps: {
        token: trailing
      },
      animated: true,
      animationType: 'fade',
      navigatorStyle: {
      navBarHidden: true,
  }
})
  }

应用启动时,将显示屏幕LoginScreen,该屏幕扩展了上方的BaseScreen.终止应用程序后,单击邮件中的URL,应用程序首先启动LoginScreen,然后将其重定向到屏幕NewPassword,完成所有操作后,我将通过以下方式重定向回LoginScreen:

this.props.navigator.resetTo({
  screen: 'LoginScreen',
  animated: true,
  overrideBackPress: true,
  animationType: 'fade',
  navigatorStyle: {
    navBarHidden: true,
  }
})

但是LoginScreenLinking.getInitialURL()仍然收到旧的url,因此它将再次重定向到NewPassword,这是一个循环.

我还尝试通过passProps: {}选项,但resetTo LoginScreen却没有运气.

我想解决此问题的唯一方法是在NewPassword屏幕中完成所有操作后,手动清除initialUrl. BaseScreen的侦听器应该存在,因为如果我不杀死应用程序(只是将其最小化),则侦听器应该正在运行以导航至NewPassword.

Wix的导航中有一个有关Deeplink的文档,我尝试将方法onNavigatorEvent(event)放入BaseScreen中,但未被调用.我不知道我是否想念什么.

谢谢您的时间.任何想法将不胜感激

解决方案

Linking.getInitialURL()当我们再次返回同一页面时为我们提供了相同的网址,要克服这一点,我们可以做一个简单的条件,即不调用DeepLink功能.像...

步骤1:首先初始化一个dummyDeepLinkedUrl字符串.

var dummyDeepLinkedUrl;

步骤2:检查是否存在类似条件,例如deeplinkUrl是否来自Linking.getInitialURL()并且deeplinkUrl不等于dummyDeepLinkedUrl.

if (url && url != dummyDeepLinkedUrl) {}

步骤3:(如果不同),请调用Deeplink函数,并将deeplinkUrl分配给dummyDeepLinkedUrl.

    this.navigateToRespectivePage(url);
    dummyDeepLinkedUrl = url;

最后,它看起来像:

Linking.getInitialURL().then(url => {
      if (url && url != dummyDeepLinkedUrl) {
        this.navigateToRespectivePage(url);
        dummyDeepLinkedUrl = url;
      }
    });

I've had this problem for like 2 weeks. I used Wix's Navigation for navigating around the app. I followed this tutorial for implementing the deeplink/universal link.

I have a base class called BaseScreen where I keep all the deeplink handler like in the tutorial. This BaseScreen would looks like this:

componentDidMount(){
    // this handles the case where the app is closed and is launched via Universal Linking.
    Linking.getInitialURL()
        .then((url) => {
          if (url) {
            // Alert.alert('GET INIT URL','initial url  ' + url)
            this.resetStackToProperRoute(url)
          }
        })
        .catch((e) => {})

   // This listener handles the case where the app is woken up from the Universal or Deep Linking
   Linking.addEventListener('url', this.appWokeUp);
  }

  componentWillUnmount(){
    // Remove the listener
    Linking.removeEventListener('url', this.appWokeUp);
  }

  appWokeUp = (event) => {
    // this handles the use case where the app is running in the background and is activated by the listener...
    // Alert.alert('Linking Listener','url  ' + event.url)
    this.resetStackToProperRoute(event.url)
  }

  resetStackToProperRoute = (url) => {
    // grab the trailing portion of the url so we can use that data to fetch proper information from the server
    let trailing = url.slice(url.lastIndexOf('=') + 1, url.length)
    // go to the desired screen with the trailing token grabbed from the url
    this.props.navigator.resetTo({
      screen: 'NewPassword',
      overrideBackPress: true,
      passProps: {
        token: trailing
      },
      animated: true,
      animationType: 'fade',
      navigatorStyle: {
      navBarHidden: true,
  }
})
  }

When the app launch, it'll show the screen LoginScreen which extends the BaseScreen above. After killing the app, click the url from the mail, the app launches LoginScreen first, then it'll redirect to the screen NewPassword, and after everything has done, I'll redirect back to LoginScreen by:

this.props.navigator.resetTo({
  screen: 'LoginScreen',
  animated: true,
  overrideBackPress: true,
  animationType: 'fade',
  navigatorStyle: {
    navBarHidden: true,
  }
})

But the Linking.getInitialURL() of the LoginScreen still receive the old url, so it'll redirect to NewPassword again, and it's a loop.

I've also tried to pass: passProps: {} option when resetTo the LoginScreen but no luck.

I guess the only way to fix it is to clear the initialUrl manually after everything's done in NewPassword screen. The listener for the BaseScreen should be there because if I don't kill the app (just minimize it), the listener should be running to navigate to NewPassword.

Wix's navigation has a doc for Deeplink, I tried putting method onNavigatorEvent(event) into the BaseScreen but it doesn't get called. I don't know if I miss something.

Thank you for your time. Any idea would be appreciated

解决方案

Linking.getInitialURL() gives us the same Url when we come back to the same page again, to Overcome this we can do a simple condition of not to call the DeepLink function. Something like...

Step 1: First init a dummyDeepLinkedUrl String .

var dummyDeepLinkedUrl;

Step 2: Check for the condition like, if deeplinkUrl is coming from Linking.getInitialURL() and deeplinkUrl is not equal to the dummyDeepLinkedUrl .

if (url && url != dummyDeepLinkedUrl) {}

Step 3: If not same call the Deeplink Function and assign the deeplinkUrl to dummyDeepLinkedUrl.

    this.navigateToRespectivePage(url);
    dummyDeepLinkedUrl = url;

Finally this will look like :

Linking.getInitialURL().then(url => {
      if (url && url != dummyDeepLinkedUrl) {
        this.navigateToRespectivePage(url);
        dummyDeepLinkedUrl = url;
      }
    });

这篇关于用于深层链接后未清除linking.getInitialURL()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆