iosobjective-cdeep-linkingmac-catalystuikitformac

How to fix custom scheme opening in maccatalyst when app is closed?


Problem & context

I am building a Maccatalyst app that uses a custom scheme (deep-link).

When my app is running and I click on the link to my app, my app opens and receives the opening url.

However, when my app is closed, and I click on the link to my app, it opens but does not receive the opening url. This issue does not occur on ios.

(FYI, the app is written with react-native 0.59, but the issue is not on the javascript side).

Can anyone help me on this ?

Code

Her is my AppDelegate.m file

#import <CoreGraphics/CGGeometry.h>
#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTLinkingManager.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                             moduleName:@"myapp"
                                            initialProperties:nil];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  // Listen to incoming app links during app execution
  return [RCTLinkingManager application:application openURL:url options:options];
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
 restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
  // Listen to universal links in iOS and tvOS
  // does not work when app is closed in debug mode
  return [RCTLinkingManager application:application
                   continueUserActivity:userActivity
                     restorationHandler:restorationHandler];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    ...
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    ...
}

@end

What I have tried


Solution

  • The answer was in the doc

    This method is not called if your implementations return NO from both the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods. (If only one of the two methods is implemented, its return value determines whether this method is called.) If your app implements the applicationDidFinishLaunching: method instead of application:didFinishLaunchingWithOptions:, this method is called to open the specified URL after the app has been initialized.

    So I need to watch for the key UIApplicationLaunchOptionsURLKey in the options of didFinishLaunchingWithOptions.

    Next time, I'll read the docs more carefully.