2013-12-16 19 views
1

當瀏覽器中的自定義URL方案單擊時,我的Mac應用程序需要打開並轉至特定頁面。要做到這一點,我添加了一個NSAppleEventManager上的applicationDidFinishLaunching除非應用程序已在運行,否則URL方案偵聽程序無法工作

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 

NSAppleEventManager *eventManager = [NSAppleEventManager sharedAppleEventManager]; 
[eventManager setEventHandler:self 
        andSelector:@selector(handleGetURLEvent:withReplyEvent:) 
       forEventClass:kInternetEventClass 
        andEventID:kAEGetURL]; 

} 

此事件管理器調用下面的選擇也是在AppDelegate中:

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent 
{ 
    NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; 
    NSLog(urlString); 
    //Parse & Do other stuff... 
} 

這工作完全在只要得到那個叫做應用程序的URL該應用程序已在運行。問題是如果應用程序未運行,則應用程序將啓動,但啓動它的URL永遠不會傳遞給handleGetURLEvent。

我確定這與在發生URL事件後調用applicationDidFinishLaunching有關。 NSNotification中的URL事件是什麼?

我知道這應該很容易,但我不知道該怎麼做。

感謝任何能夠提供幫助的人。

回答

1

嘗試將setEventHandler放入代理的awakeFromNib中

+0

工作正常。謝謝 – centreee

相關問題