Reputation: 2644
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.
Upvotes: 7
Views: 6091
Reputation: 97
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!
Upvotes: 0
Reputation: 799
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.
Upvotes: 4