我启用了深度链接,应用程序打开时一切正常。当我使用 url moderatorapp://hello 从关闭状态打开应用程序时,它会记录正确的 url,但是当应用程序在从后台状态打开时深度链接时它不起作用。我的代码如下:
componentDidMount() {
// Storage.clear();
Storage.getItem('data_moderator')
.then(_data => {
if (_data && _data.tokens) {
this.autoLogin(_data.tokens);
} else {
Actions.loginForm();
}
}
);
Linking.getInitialURL()
.then(url => {
console.log('Initial Url then ', url);
if (url) {
console.log('Initial Url ', url);
}
})
.catch(error => console.log(error));
Linking.addEventListener('url', this.handleOpenURL);
}
这显然是因为此时没有调用 componentDidMount 方法。
我尝试过的:
我试图将链接代码包装在一个事件中,该事件检测到应用程序进入事件状态但它不起作用,它记录了与应用程序关闭时的初始尝试相同的 url。当我尝试使用 url moderatorapp://goodbye 从后台状态深入链接到应用程序时,它会记录 moderatorapp://hello。所以它不知何故没有更新。
AppState.addEventListener('change', (state) => {
if (state === 'active') {
console.log('state active');
Linking.getInitialURL()
.then(url => {
console.log('Initial Url then ', url);
if (url) {
console.log('Initial Url ', url);
}
})
.catch(error => console.log(error));
}
if(state === 'background'){
console.log('background');
}
});
我是 React Native 的新手,非常感谢任何帮助。
谢谢。
最佳答案
https://facebook.github.io/react-native/docs/linking.html具体来说:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [RCTLinkingManager application:application openURL:url options:options];
}
Apple 更改了用于链接的 api,因此如果您的目标是 ios 9 或更新版本,您需要在 AppDelegate.m 文件中使用此代码。
关于javascript - React Native 深层链接应用程序从后台打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48713074/