【问题标题】:Linking.getInitialURL() is not being cleared after used for deeplinkLinking.getInitialURL() 用于深度链接后未清除
【发布时间】:2019-03-24 13:11:20
【问题描述】:

这个问题我已经有 2 周的时间了。我使用Wix's Navigation 在应用程序中导航。我跟着this tutorial 实现了深度链接/通用链接。

我有一个名为BaseScreen 的基类,我在其中保留了教程中的所有深度链接处理程序。这个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 by:

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 但它没有被调用。我不知道我是否错过了什么。

感谢您的宝贵时间。任何想法将不胜感激

【问题讨论】:

标签: android ios react-native deeplink


【解决方案1】:

Linking.getInitialURL() 当我们再次回到同一个页面时给我们同样的Url,为了克服这个我们可以做一个不调用DeepLink函数的简单条件。比如……

第一步:首先初始化一个dummyDeepLinkedUrl String。

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;
      }
    });

【讨论】:

  • 如果我需要返回相同的 URL,并且需要进行重定向,那么它会起作用吗?
  • 这不会清除链接并允许使用相同的 URL 进行第二次启动。
【解决方案2】:

有两种方法可以处理打开您的应用的 URL。

  1. 如果应用程序已打开,则应用程序处于前台并触发 Linking 事件您可以使用以下命令处理这些事件 Linking.addEventListener(url, callback)。

  2. 如果应用程序尚未打开,则将其打开并将 url 作为初始 URL 传入您可以使用以下方式处理这些事件 Linking.getInitialURL(url) - 它返回一个 Promise 解析为 网址,如果有的话。

您可以阅读more detail 这是一个例子

export default class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
      initialised: false
    }
  }

  componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);
    Linking.addEventListener('url', event => {
       console.log('deep link from background', event.url)
    })
  }

  _handleAppStateChange = async (nextAppState) => {
    const url = await Linking.getInitialURL();
    if (url !== null && !this.state.initialised) {
      this.setState({ initialised: true })
      console.log('deep link from init app', url)
    }
  }

  componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppStateChange);
    Linking.removeEventListener('url')
  }
}

【讨论】:

    猜你喜欢
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2013-07-17
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多