gpt4 book ai didi

android - Linking.getInitialURL() 在用于深度链接后未被清除

转载 作者:行者123 更新时间:2023-11-29 14:43:11 28 4
gpt4 key购买 nike

我已经遇到这个问题大约 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:

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

但是 LoginScreenLinking.getInitialURL() 仍然接收到旧的 url,所以它会再次重定向到 NewPassword,并且这是一个循环。

我也试过在 resetTo LoginScreen 时传递:passProps:{} 选项,但没有成功。

我想修复它的唯一方法是在 NewPassword 屏幕中完成所有操作后手动清除 initialUrl。 BaseScreen 的监听器应该在那里,因为如果我不终止应用程序(只是将其最小化),监听器应该运行以导航到 NewPassword

Wix 的导航有一个 Deeplink 文档,我尝试将方法 onNavigatorEvent(event) 放入 BaseScreen 但它没有被调用。我不知道我是否错过了什么。

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

最佳答案

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

第 1 步:首先初始化一个虚拟的 DeepLinkedUrl 字符串。

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

关于android - Linking.getInitialURL() 在用于深度链接后未被清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52895818/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com