3

I have successfully implemented a universal link that opens up a specific page in my app (if the app is turned off). The problem is that if the app is running in the background, the eventListener is not being called. Here is the code:

import {Linking} from 'react-native';
export default class App extends React.Component {

    async componentDidMount(){
        Linking.addEventListener('url', this._handleOpenURL);
        let url = await Linking.getInitialURL();
        if (url) {
            console.log('MOUNT GET INIT URL','initial url  ' + url);
        }
    }
    _handleOpenURL = (event) => {
        console.log("in _handleOpenURL", event.url)
    }
}

MOUNT GET INIT URL is successfully logged to the console. in _handleOpenURL is never logged. It seems other people on the internet have had this problem but no one has answered it. Does anyone know what to do?

Philip7899
  • 4,599
  • 4
  • 55
  • 114

1 Answers1

4

Add this just before the last @end in appdelegate.m:

// iOS 9.x or newer


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

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
 restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
 return [RCTLinkingManager application:application
                  continueUserActivity:userActivity
                    restorationHandler:restorationHandler];
}
Philip7899
  • 4,599
  • 4
  • 55
  • 114
  • Just adding that it is necessary to `#import ` on the start of the file. – Dante Oct 20 '22 at 13:42
  • Implemented this but addEventListener still not working. Am I possibly misting something subtle ? – Lev Dec 21 '22 at 15:39