问题&上下文
我正在构建一个使用自定义方案(深度链接(的Maccatalyst应用程序。
当我的应用程序运行时,我点击应用程序的链接,我的应用会打开并接收打开的url。
然而,当我的应用程序关闭,我点击应用程序的链接时,它会打开,但不会收到打开的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
我尝试过的
我检查了问题不在javascript方面,因为当应用程序关闭时,无论是application:openURL:options还是application:continueUserActivity:restorationHandler(来自应用程序委托(都不会被调用。
正如这里所解释的,我试图查看didFinishLaunchingWithOptions中的launchOptions中是否发送了任何内容。但这显然只是在第一次发射时才被要求的。
我看过官方文件,但找不到解决问题的办法。
答案在文档中
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
。
下次,我会更仔细地阅读文档。