如何通过Swift中的URLScheme捕获用于启动OS X应用程序的URL? [英] How to capture the URL used to launch an OS X application via URLScheme in Swift?

查看:248
本文介绍了如何通过Swift中的URLScheme捕获用于启动OS X应用程序的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在Swift的Objective-C中复制执行此操作的常用方法,以用于我正在使用的新Swift应用.

I've been trying to replicate the usual way of doing this in Objective-C in Swift for a new swift app I'm playing around with.

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

How to do this in Objective-C is well documented, you get the shared apple event manager and call setEventHandler method to register a function as the handler for the event class kInternetEventClass with the event id kAEGetURL.

因此,我试图通过将代码添加到全新项目中的AppDelegate.swift模板中来做同样的事情:

So in swift I'm trying to do the same thing with this code added to the template AppDelegate.swift inside a brand new project:

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方法的forEventClassandEventId参数上都遇到类型错误:

As far as I can tell this is just a syntax conversion of the standard Objective-c call. But I get a type error for both the forEventClass and andEventId arguments to the setEventHandler method:

'NSNumber' is not a subtype of 'AEEventClass'用于forEventClass参数

和:

'NSNumber' is not a subtype of 'AEEventId'用于andEventID参数

我不确定在这个阶段我做错了什么,因为kInternetEventClasskAEGetURL都是苹果定义的常量...当然,我不需要将它们的NSNumber类型转换为它们各自的类型必需的类型?如果我是我,我将不知道怎么做.

I'm not sure what I'm doing wrong at this stage as both kInternetEventClass and kAEGetURL are constants defined by apple... Surely I'm not required to convert their NSNumber type to their respective required types? And if I am I can't work out how.

推荐答案

据我从文档中得知,类采用常量并将其转换为正确的类型:

As far as I can tell from the documentation the classes takes the constant and converts it to the proper type:

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

这篇关于如何通过Swift中的URLScheme捕获用于启动OS X应用程序的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆