javascript - React Native 深层链接应用程序从后台打开

标签 javascript react-native react-native-ios

我启用了深度链接,应用程序打开时一切正常。当我使用 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/

相关文章:

javascript对象删除元素

javascript - 在新窗口中打开页面上的 div,同时保持通过 websocket 更新它的能力

javascript - 更新 React [Native] 查看日变化

ios - 在 iOS 应用程序上使用 Google map (应用程序评论)

javascript - 在 HTML5 Canvas 中更改描边颜色

javascript - 在 D3 V4 中添加自定义画笔 handle ?

node.js - React-Native 打包程序失败 : Duplicate module name

react-native - 如何以编程方式在 react-native 中截取屏幕截图

react-native - React Native 中的 RCTBridge

ios - 如何在react-native中调试 `native Objective C code`?