Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native deep link app opening from background

Ive enabled deep linking and everything works great when the application opens. When I open the app from a closed state using the url moderatorapp://hello it logs the correct url, but it does not work when the app is deep linked while being opened from a background state. My code is as follows:

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

This is obviously because the componentDidMount method is not being called at that point.

What I have tried:

I attempted to wrap the Linking code inside of an event that detects the application entering into the active state and it doesn't work, it logs the same url from the initial attempt when the app was closed. When I attempt to deep link into the app from the background state using the url moderatorapp://goodbye it logs the moderatorapp://hello. So it somehow is not updating.

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

Im really new to React Native, any assistance would be greatly appreciated.

Thanks.

like image 650
Aaron Avatar asked Feb 09 '18 19:02

Aaron


1 Answers

https://facebook.github.io/react-native/docs/linking.html Specifically:

- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
   {
return [RCTLinkingManager application:application openURL:url options:options];
}

Apple changed the api for linking so if you are targeting ios 9 or newer, you need this code in your AppDelegate.m file.

like image 163
prog_24 Avatar answered Nov 08 '22 01:11

prog_24