Philip7899
Philip7899

Reputation: 4677

Linking.addEventListener not working in react native

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?

Upvotes: 3

Views: 3104

Answers (1)

Philip7899
Philip7899

Reputation: 4677

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

Upvotes: 4

Related Questions