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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…