【发布时间】:2011-09-27 13:07:59
【问题描述】:
我正在尝试为我的应用程序实现自定义 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 在我之前处理了它,但我找不到解决该问题的方法。
【问题讨论】:
-
听 NSApplicationDidBecomeActiveNotification 通知似乎可以解决问题。我认为这不是最好的方法,所以我将这个问题留给一些更好的想法:)