TwitterAPI 0.4.0

TwitterAPI 0.4.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2016年11月
SwiftSwift 版本3.0
SPM支持 SPM

Shinichiro Aska 维护。



 
依赖关系
OAuthSwift= 1.0.0
MutableDataScanner>= 0
 

Build Status

此 Twitter 框架旨在支持 OAuth 和 Social.framework,可以处理 REST 和 Streaming API。

⚠️ 开发者发布

功能

  • 使用 NSURLSession 连接 Streaming API
  • 同时支持 OAuth 和 Social.framework(仅限 iOS)
  • 同时支持 iOS 和 macOS

用法

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()

REST API

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

    }

使用 Alamofire

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

    }

如何获取客户端对象

通过 OAuth

import TwitterAPI

let client = OAuthClient(
    consumerKey: "",
    consumerSecret: "",
    accessToken: "",
    accessTokenSecret: "")

通过 Social.framework

import Accounts
import TwitterAPI

let client = AccountClient(account: account)

序列化 / 反序列化

保存和加载可以是,例如,使用 keychain。

let string = client.serialize

let client = ClientDeserializer.deserialize(client.serialize)

如何使用 Social.framework

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

        }
}

如何使用 OAuthSwift

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
    }
}

要求

  • iOS 9.0+ / Mac OS X 10.10+
  • Swift 3 和 Xcode 8

许可证

TwitterAPI 在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE。