CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.
TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Nov 2016 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Shinichiro Aska.
Depends on: | |
OAuthSwift | = 1.0.0 |
MutableDataScanner | >= 0 |
This Twitter framework is to both support the OAuth and Social.framework, can handle REST and Streaming API.
import TwitterAPI
import SwiftyJSON
let request = client
.streaming("https://userstream.twitter.com/1.1/user.json")
.progress({ (data: NSData) -> Void in
// The already divided by CRLF ;)
// https://dev.twitter.com/streaming/overview/processing
let json = JSON(data: data)
})
.completion({ (responseData: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
})
.start()
// disconnect
request.stop()
let parameters = [String: String]()
client
.get("https://api.twitter.com/1.1/statuses/home_timeline.json", parameters: parameters)
.response {
(responseData: NSData?, response: NSHTTPURLResponse?, error: NSError?) -> Void in
}
// Without parameters
client
.get("https://api.twitter.com/1.1/statuses/home_timeline.json")
.response {
(responseData: NSData?, response: NSHTTPURLResponse?, error: NSError?) -> Void in
}
// POST
client
.post("https://api.twitter.com/1.1/statuses/update.json", parameters: parameters)
.response {
(responseData: NSData?, response: NSHTTPURLResponse?, error: NSError?) -> Void in
}
let client = OAuthClient(
consumerKey: "",
consumerSecret: "",
accessToken: "",
accessTokenSecret: "")
let url = "https://api.twitter.com/1.1/statuses/update.json"
let parameters = ["status": "Alamofire"]
Alamofire
.request(client.makeRequest(.POST, url: url, parameters: parameters))
.reponseJSON { response in
}
import TwitterAPI
let client = OAuthClient(
consumerKey: "",
consumerSecret: "",
accessToken: "",
accessTokenSecret: "")
import Accounts
import TwitterAPI
let client = AccountClient(account: account)
Saving and loading can be, for example, using a keychain.
let string = client.serialize
let client = ClientDeserializer.deserialize(client.serialize)
import Accounts
import TwitterAPI
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 message = error.description
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
return
}
let accounts = accountStore.accountsWithAccountType(accountType) as! [ACAccount]
guard let account = accounts.first else {
let message = "There are no Twitter accounts configured. You can add or create a Twitter account in Settings."
let alert = UIAlertController(title: "Error", message: message,
preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
return
}
let client = AccountClient(account: account)
client
.get("https://api.twitter.com/1.1/statuses/home_timeline.json")
.response {
(responseData: NSData?, response: NSHTTPURLResponse?, error: NSError?) -> Void in
}
}
import OAuthSwift
import TwitterAPI
let oauthswift = OAuth1Swift(
consumerKey: "YOUR_APP_CONSUMER_KEY",
consumerSecret: "YOUR_APP_CONSUMER_SECRET",
requestTokenUrl: "https://api.twitter.com/oauth/request_token",
authorizeUrl: "https://api.twitter.com/oauth/authorize",
accessTokenUrl: "https://api.twitter.com/oauth/access_token"
)
oauthswift.authorizeWithCallbackURL(NSURL(string: "yourappscheme://success")!,
success: { (credential, response) -> Void in
let client = OAuthClient(
consumerKey: "YOUR_APP_CONSUMER_KEY",
consumerSecret: "YOUR_APP_CONSUMER_SECRET",
accessToken: credential.oauth_token,
accessTokenSecret: credential.oauth_token_secret)
}) { (error) -> Void in
let message = error.description
let alert = UIAlertController(title: "Error", message: message,
preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
// AppDelegate.swift
import UIKit
import OAuthSwift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, openURL url: NSURL,
sourceApplication: String?, annotation: AnyObject) -> Bool {
if url.absoluteString.hasPrefix("yourappscheme://success") {
OAuth1Swift.handleOpenURL(url)
}
return true
}
}
TwitterAPI is released under the MIT license. See LICENSE for details.