skip to Main Content

When opening my app in simulator via a deeplink, url event never fires.

Note that getInitialurl works if app was closed.
But if app is already open and I run npx uri-scheme open "mychat://bar" --ios, app focuses but no url event fires…

Anyone had this problem ?

I’m running XCode 13.4.1
MacOS 12.5.1
React Native 0.70

repo to reproduce bug

2

Answers


  1. The event needs some additional configuration within your AppDelegate.m file in order to emit the events as mentioned in the docs. Either open your Project from XCode and edit AppDelegate.m or open ./ios/{YOUR_PROJECT_NAME}/AppDelegate.m (or AppDelegate.mm) file and add the following lines at the end of the file before the @end tag comes:

    // Add this inside `@implementation AppDelegate` above `@end`:
    - (BOOL)application:(UIApplication *)application
       openURL:(NSURL *)url
       options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
    {
      return [RCTLinkingManager application:application openURL:url options:options];
    }
    
    // Add this inside `@implementation AppDelegate` above `@end`:
    - (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
     restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
    {
     return [RCTLinkingManager application:application
                      continueUserActivity:userActivity
                        restorationHandler:restorationHandler];
    }
    
    @end 
    

    Important: If you get this working, you have two ways of handling the deeplink event and you have to handle them seperately! I think the second one will help you to solve your problem.

    1 – The app is closed and will open by the deeplink:

    Linking.getInitialURL().then(url => { 
       if(url != null) {
           //DoSomethingWithUrl
       }
    });
    

    2 – The app is already running and will be focused using deeplink:

    Linking.addEventListener('url',(url)=>{ 
       if(url != null) {
           //DoSomethingWithUrl
       }
    });
       
    

    Putting these lines within your apps view and assumed your app has some sort of state (e.g. using the useState hook or redux) it’ll called on every state change that occurs, due to everything beside the state itself will be re-rendered on changing the state. Therefore I would suggest you to call these two methods only one time when app is starting and you can accomplish that by doing so:

    const [isInitialStart, setInitialStart] = useState(true);
    if(isInitialStart){
        Linking.getInitialURL().then(url => { 
            if(url != null) {
                //DoSomethingWithUrl
            }
        });
        Linking.addEventListener('url',(url)=>{ 
            if(url != null) {
                //DoSomethingWithUrl
            }
        });
        setInitialStart(false); 
    }
    

    I hope this will help you to solve your problem.

    Login or Signup to reply.
  2. In my case, I was adding the required code block of AppDelegate below @interface AppDelegate, but have to add inside @implementation AppDelegate which below interface.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search