最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native deep link app opening from background - Stack Overflow

programmeradmin3浏览0评论

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:

ponentDidMount() {
    // 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 ponentDidMount 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.

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:

ponentDidMount() {
    // 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 ponentDidMount 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.

Share Improve this question asked Feb 9, 2018 at 19:47 Aaron BalthaserAaron Balthaser 2,6543 gold badges39 silver badges63 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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.

The deep linking is working as expected for me even the app is in background. Please check the below specifications.

Node Version : v12.18.x OR Greater NPM Version : v6.14.x OR Greater react-native-cli : 2.0.1 react-native : 0.63.x OR Greater

Please check if you have added below line in your AppDelegate.m.

#import <React/RCTLinkingManager.h>

It must be added above #ifdef FB_SONARKIT_ENABLED line. Adding it below this line will cause failing of build while Archiving it for release.

Please check if you have added below code in your AppDelegate.m which is responsible for deep linking.

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

It will work for app cold boot, but it will not work if your app is in background. For this, you need to add below code in your AppDelegate.m

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray<id> *_Nullable))restorationHandler { return [RCTLinkingManager application:application continueUserActivity:userActivity restorationHandler:restorationHandler]; }

This should work irrespective of your AppState: active **OR** background.

This worked for me as expected! Give it a try. This is should definitely work.

Thanks in advance!

发布评论

评论列表(0)

  1. 暂无评论