macos – 如何通过Swift中的URLScheme捕获用于启动OSX应用程序的
发布时间:2020-12-14 02:28:56 所属栏目:百科 来源:网络整理
导读:我一直在尝试复制通常在 Objective-c中使用swift进行此操作的方法,以便我正在玩一个新的快速应用程序. 如何在Objective-c中做到这一点有详细记录,您将获得共享的apple事件管理器并调用setEventHandler方法将函数注册为事件类kInternetEventClass的处理程序,
我一直在尝试复制通常在
Objective-c中使用swift进行此操作的方法,以便我正在玩一个新的快速应用程序.
如何在Objective-c中做到这一点有详细记录,您将获得共享的apple事件管理器并调用setEventHandler方法将函数注册为事件类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"); } 据我所知,这只是标准Objective-c调用的语法转换.但是我得到了setEventHandler方法的forEventClass和andEventId参数的类型错误: ‘NSNumber’不是forEventClass参数的’AEEventClass’的子类型 和: ‘NSNumber’不是andEventID参数的’AEEventId’的子类型 我不确定在这个阶段我做错了什么因为kInternetEventClass和kAEGetURL都是由apple定义的常量…当然我不需要将他们的NSNumber类型转换为各自所需的类型?如果我是,我无法弄清楚如何.
据我从文档中可以看出,类获取常量并将其转换为正确的类型:
func applicationWillFinishLaunching(aNotification: NSNotification?) { var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager() appleEventManager.setEventHandler(self,forEventClass: AEEventClass(kInternetEventClass),andEventID: AEEventID(kAEGetURL)) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |