Skip to content

Instantly share code, notes, and snippets.

@thinkclay
Last active August 29, 2015 14:03
Show Gist options
  • Save thinkclay/0c4ffd7e61c5b63fc4e3 to your computer and use it in GitHub Desktop.
Save thinkclay/0c4ffd7e61c5b63fc4e3 to your computer and use it in GitHub Desktop.
import UIKit
import Accounts
import Social
import SwifteriOS
class AuthViewController: UIViewController {
var swifter: Swifter
// Default to using the iOS account framework for handling twitter auth
let useACAccount = true
required init(coder aDecoder: NSCoder) {
self.swifter = Swifter(consumerKey: "RErEmzj7ijDkJr60ayE2gjSHT", consumerSecret: "SbS0CHk11oJdALARa7NDik0nty4pXvAxdt7aj0R5y1gNzWaNEx")
super.init(coder: aDecoder)
}
@IBAction func didTouchUpInsideLoginButton(sender: AnyObject) {
let failureHandler: ((NSError) -> Void) = {
error in
self.alertWithTitle("Error", message: error.localizedDescription)
}
if useACAccount {
let accountStore = ACAccountStore()
let accountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)
// Prompt the user for permission to their twitter account stored in the phone's settings
accountStore.requestAccessToAccountsWithType(accountType, options: nil) {
granted, error in
if granted {
let twitterAccounts = accountStore.accountsWithAccountType(accountType)
if twitterAccounts?.count == 0
{
self.alertWithTitle("Error", message: "There are no Twitter accounts configured. You can add or create a Twitter account in Settings.")
}
else {
let twitterAccount = twitterAccounts[0] as ACAccount
self.swifter = Swifter(account: twitterAccount)
self.fetchTwitterHomeStream()
}
}
else {
self.alertWithTitle("Error", message: error.localizedDescription)
}
}
}
else {
swifter.authorizeWithCallbackURL(NSURL(string: "swifter://success")!, success: {
accessToken, response in
self.fetchTwitterHomeStream()
},failure: failureHandler
)
}
}
func fetchTwitterHomeStream() {
let failureHandler: ((NSError) -> Void) = {
error in
self.alertWithTitle("Error", message: error.localizedDescription)
}
self.swifter.getStatusesHomeTimelineWithCount(20, sinceID: nil, maxID: nil, trimUser: true, contributorDetails: false, includeEntities: true, success: {
(statuses: [JSONValue]?) in
// Successfully fetched timeline, so lets create and push the table view
let tweetsViewController = self.storyboard!.instantiateViewControllerWithIdentifier("TweetsViewController") as TweetsViewController
if statuses != nil {
tweetsViewController.tweets = statuses!
self.presentViewController(tweetsViewController, animated: true, completion: nil)
}
}, failure: failureHandler)
}
func alertWithTitle(title: String, message: String) {
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
@dctamtn
Copy link

dctamtn commented Dec 5, 2014

It is not return callback function of the app at this code, please help :

swifter.authorizeWithCallbackURL(NSURL(string: "swifter://success")!, success: {
accessToken, response in

    self.fetchTwitterHomeStream()

    },failure: failureHandler
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment