2015-12-14 101 views
1

我有一个使用自定义URL方案在Swift中编写的Mac应用程序。获取用于启动OS X应用程序的初始URL

如果应用程序正在运行并从链接打开,则一切正常。但是,如果应用程序没有运行,那么应用程序无法打开正确的页面。

我注册的通知是这样的:

let eventManager = NSAppleEventManager.sharedAppleEventManager() 
eventManager.setEventHandler(self, andSelector: "handleGetURLEvent:replyEvent:", forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL)) 

我如何从最初推出的网址:

func applicationDidFinishLaunching(aNotification: NSNotification) 

编辑1: 在我的应用程序委托我:

func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) 

该函数被调用,并且在应用程序已经打开时工作,但是当应用程序关闭时无法工作。

回答

0

你快到了!

Looking at a tutorial article you might be looking at at,您仍然需要实施处理“kAEGetURL”Apple事件的方法。

在您的应用程序委托,创建一个类似如下的方法:

func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) { 
    if let aeEventDescriptor = event?.paramDescriptorForKeyword(AEKeyword(keyDirectObject)) { 
     if let urlStr = aeEventDescriptor.stringValue { 
      let url = NSURL(string: urlStr) 

      // do something with the URL 
     } 
    } 
} 
+0

我有一个功能,在解决它我的应用程序委托,但它没有正确触发。 –

+0

你在哪里注册了你的'setEventHandler'? –

+0

in func applicationDidFinishLaunching(aNotification:NSNotification) –

1

我有完全相同的问题,并通过移动注册调用到

applicationWillFinishLaunching(_ notification: Notification) 
相关问题