我正在尝试为我的应用程序实现自定义URL方案。我已添加了Info.plist所需的行。在调用指定的URL(例如:myapp://)后,应用程序会启动。
如果我想处理URL,我找到了以下步骤:
@interface EventHandler : NSObject {
}
@end
@implementation EventHandler
- (id)init {
self = [super init];
if (self) {
NSLog(@"eventHandler::init");
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(applicationDidFinishLaunching:)
// name:NSApplicationWillFinishLaunchingNotification
name:NSApplicationDidFinishLaunchingNotification
object:nil];
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
}
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
NSLog(@"%@", url);
}
@end
如果应用程序正在运行,则上述代码有效,但是如果调用URL并且应用程序已终止,则不会捕获事件。我认为这是因为 NSApplicationDidFinishLaunchingNotification。将其更改为 NSApplicationWillFinishLaunchingNotification 导致未捕获任何事件。也许Qt在我之前处理了它,但我找不到解决问题的方法。