关闭应用程序时如何修复在 maccatalyst 中打开的自定义方案?

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

问题与背景

我正在构建一个使用自定义方案 (deep-link) 的 Maccatalyst 应用程序。

当我的应用程序是 运行 并且我点击我的应用程序的 link 时,我的应用程序打开并接收到开头 url。

然而,当我的应用程序关闭时,我点击了我的应用程序的 link,它打开但没有接收到打开 url。 ios.

上不会出现此问题

(仅供参考,该应用程序是使用 react-native 0.59 编写的,但问题不在 javascript 方面)。

谁能帮我解决这个问题?

代码

她是我的AppDelegate.m档案

#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

我试过的

答案在 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.

所以我需要注意 didFinishLaunchingWithOptions 选项中的 UIApplicationLaunchOptionsURLKey 键。

下次我会更仔细地阅读文档。