Search code examples
iosxcodeswifttwitter

How to Authorize Twitter with Swifter


Could someone please explain to me how to successfully authorize Twitter (oAuth) with Swifter for iOS. I am trying to create a timeline.

let twitterAccount = Swifter(consumerKey: "CONSUMER-KEY", consumerSecret: "CONSUMER-SECRET")

Then I need to authorize, and I'm quite confused on how to do so.

swifter.authorizeWithCallbackURL(callbackURL, success: {
(accessToken: SwifterCredential.OAuthAccessToken?, response: NSURLResponse) in

// ...

},
failure: {
    (error: NSError) in

    // ...

})

I get an error saying 'Unresolved Identifier of callbackURL' which is obviously because I haven't defined it, but when I do it says 'extra argument in call'. Also is there any easier way of Authorizing? After this how am I supposed to get the users screen-name/user-id to use in the get-home-timeline part. I know I'm doing something completely wrong and I am quite new to all of this, so any help would be appreciated.


Solution

  • Maybe you have found an answer, but I am writing here for those who are still looking for it.

    swifter.authorizeWithCallbackURL(NSURL(string: "swifter://success")!, success: {
                (accessToken: SwifterCredential.OAuthAccessToken?, response: NSURLResponse) in
    
                println("Access Token key \(accessToken?.key)")
                println("userId\(accessToken?.userID)")
                println("userName\(accessToken?.screenName)")
                println("Access Token secret\(accessToken?.secret)")
                //Save the values that you need in NSUserDefaults 
                },
                failure: failureHandler)
    

    You can use any callbackURL that you like. But for your app to respond to this URL you must do some changes. In the AppDelegate class add the following. Don't forget to import SwifteriOS

    func application(application: UIApplication!, openURL url: NSURL!, sourceApplication: String!, annotation: AnyObject!) -> Bool {
            Swifter.handleOpenURL(url)
    
            return true
        }
    

    Now click on your project in the Project Explorer, select the info tab. At the bottom you will see URL Types. Expand it and click the plus sign. In the URL Schemes enter your callbackURL scheme. In this example its swifter and in identifier write your app identifier.

    Now in any other ViewController you need to call following before you can call any API that need Authentication.

    required init(coder aDecoder: NSCoder) {
    
            var oauthToken : String = NSUserDefaults.standardUserDefaults().valueForKey("oauth_token") as String
            var oauthTokenSecret : String  = NSUserDefaults.standardUserDefaults().valueForKey("oauth_secret") as String
    
            self.swifter = Swifter(consumerKey: Twitter["consumerKey"]!, consumerSecret: Twitter["consumerSecret"]!, oauthToken: oauthToken, oauthTokenSecret: oauthTokenSecret)
            super.init(coder: aDecoder)
        }
    

    After this you can send message, post a tweet, create friendship etc.