如何通过Swift中的URLScheme捕获用于启动OSX应用程序的URL?

Mik*_*ver 6 macos swift xcode6

我一直在尝试复制通常在Objective-c中使用swift进行此操作的方法,以便我正在玩一个新的快速应用程序.

如何在Objective-c中执行此操作setEventHandler已有详细记录,您将获得共享的apple事件管理器和调用方法,以将事件注册为kInternetEventClass具有事件ID 的事件类的处理程序kAEGetURL.

所以在swift中我试图用这个代码添加到一个全新项目中的模板AppDelegate.swift中做同样的事情:

func applicationWillFinishLaunching(aNotification: NSNotification?) {
    var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager()
    appleEventManager.setEventHandler(self, andSelector: "handleGetURLEvent:withReplyEvent:", forEventClass: kInternetEventClass, andEventID: kAEGetURL)
}

func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
    println("yay");
}
Run Code Online (Sandbox Code Playgroud)

据我所知,这只是标准Objective-c调用的语法转换.但是我得到了方法的参数forEventClassandEventId参数的类型错误setEventHandler:

'NSNumber' is not a subtype of 'AEEventClass'为了forEventClass论证

和:

'NSNumber' is not a subtype of 'AEEventId'为了andEventID论证

我不知道我在做什么错在这个阶段既是kInternetEventClasskAEGetURL是由苹果定义的常量......当然我不要求他们转换NSNumber类型各自需要的类型?如果我是,我无法弄清楚如何.

Mag*_*alp 15

据我从文档中可以看出,类获取常量并将其转换为正确的类型:

func applicationWillFinishLaunching(aNotification: NSNotification?) {
    var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager()
    appleEventManager.setEventHandler(self, andSelector: "handleGetURLEvent:withReplyEvent:", forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}
Run Code Online (Sandbox Code Playgroud)

  • 值得一提的是,您的选择器字符串应为“ handleGetURLEvent:replyEvent:”,以正确匹配上述Swift功能声明。 (2认同)