Aar*_*ron 5 javascript react-native react-native-ios
Ive启用了深层链接,当应用程序打开时,一切正常。当我使用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);
}
Run Code Online (Sandbox Code Playgroud)
显然这是因为此时未调用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');
}
});
Run Code Online (Sandbox Code Playgroud)
我对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];
}
Run Code Online (Sandbox Code Playgroud)
Apple 更改了链接的 api,因此如果您的目标是 ios 9 或更高版本,则在 AppDelegate.m 文件中需要此代码。