Chris
Chris

Reputation: 3795

Twitter OAuth Swift OS X

I'm trying to integrate Twitter OAuth into my OS X app with Swift. I am getting a request token from the Twitter API and then using the app's protocol as the callbackURL appname://oauth.

I have seen a tutorial for iOS that uses something like this and then passes the request token from the url to then get the access token.

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

Which was added to AppDelegate.swift.

As I understand, this won't work as Cocoa doesn't have UIApplication.

Can anyone help me adjust the above code for OS X?

Upvotes: 0

Views: 440

Answers (1)

Jeremy Pope
Jeremy Pope

Reputation: 3352

You still have to register your app's protocol (urlScheme) in its info.plist file. Assuming you have done that. You'll want to do something like the following in the app delegate:

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

Where Selector("handleAppleEvent") maps to a function name

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

Upvotes: 1

Related Questions