测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2016年11月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Shinichiro Aska 维护。
依赖关系 | |
OAuthSwift | = 1.0.0 |
MutableDataScanner | >= 0 |
此 Twitter 框架旨在支持 OAuth 和 Social.framework,可以处理 REST 和 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)
保存和加载可以是,例如,使用 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 在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE。