推特 OAuth Swift OS X

Twitter OAuth Swift OS X

我正在尝试将 Twitter OAuth 集成到我的带有 Swift 的 OS X 应用程序中。我从 Twitter API 获取请求令牌,然后使用应用程序的协议作为回调 URL appname://oauth

我看过 iOS 的教程,它使用类似的东西,然后从 url 传递请求令牌,然后获取访问令牌。

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
    // Fetch access token
    return true
}

已添加到 AppDelegate.swift

据我了解,这行不通,因为 Cocoa 没有 UIApplication

谁能帮我调整上面的代码 OS X?

您仍然需要在其 info.plist 文件中注册您应用的协议 (urlScheme)。假设你已经这样做了。您需要在应用程序委托中执行类似以下操作:

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

其中 Selector("handleAppleEvent") 映射到一个函数名

func handleEvent(event: NSAppleEventDescriptor!, withReplyEvent: NSAppleEventDescriptor!) {
    println("Check the event for info")
}