2

In my React Native app, I had to migrate its AppDelegate from Objective-C to Swift. After the migration, deeplinks stopped working: when a link is used, the app opens but it does not navigate to the desired route, while in Android it works fine.

Following React Navigation docs, I had this code in my AppDelegate.m:

#import <React/RCTLinkingManager.h> 
// other imports

@implementation AppDelegate

// other stuff

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

  if ([RCTLinkingManager application:application openURL:url options:options]) {
    return YES;
  }

  return NO;
}

- (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

which became the following in Swift:

// imports

@main

// other stuff

func application(_ application: UIApplication, open URL: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if (ApplicationDelegate.shared.application(application, open: URL, options: options)) {
      return true
    }

    if (RCTLinkingManager.application(application, open: URL, options: options)) {
      return true;
    }

    return false
  }

  func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    return RCTLinkingManager.application(application, continue: userActivity, restorationHandler: restorationHandler)
  }

I also have a myApp-Bridging-Header.h file:

// other imports
#import <React/RCTLinkingManager.h>

I don't know if I'm doing something wrong (as I'm pretty lost with Objective-C or Swift) or if deeplinks with an AppDelegate written in Swift just don't work.

Update

The problem seems related with scenes. After adding this code to my PhoneScene, deepLinks work, but only if the app is open or in the background; if it's closed it opens, but in home screen.

func scene( _ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        return
    }

    if RCTLinkingManager.application(UIApplication.shared, open: url, sourceApplication: nil, annotation: [UIApplication.OpenURLOptionsKey.annotation]) {
        return
    }
  }

  func scene( _ scene: UIScene, continue userActivity: NSUserActivity) {
      RCTLinkingManager.application(UIApplication.shared, continue: userActivity, restorationHandler: { _ in })
  }
4
  • hmm hard to say form what you posted, but this should be in class AppDelegate: NSObject, UIApplicationDelegate - is it? also not sure what @main is doing there - did you mean @UIApplicationMain? Deep links in swift definitely work though Commented Jun 16, 2023 at 16:16
  • Sorry, but I have no experience at all with Swift nor Objective-C. Code is translated from Objective-C to Swift with ChatGPT. I've swapped @main with @UIApplicationMain but nothing changed. Commented Jun 19, 2023 at 15:19
  • my suggestion would be to run proper app generation with RN on the side, and use a generated skeleton for your app, instead of trying to use ChatGPT Commented Jun 19, 2023 at 18:00
  • App was generated with RN, but it created an Objective-C AppDelegate. I've been forced to switch to swift due to make a specific library to work. Commented Jun 22, 2023 at 11:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.