Swifter - A Twitter framework for iOS & OS X written in Swift

Related tags

API Wrapper Swifter
Overview

Swifter

Carthage compatible

Getting Started

Installation

If you're using Xcode 6 and above, Swifter can be installed by simply dragging the Swifter Xcode project into your own project and adding either the SwifteriOS or SwifterMac framework as an embedded framework.

Usage

Swifter can be used with the 3 different kinds of authentication protocols Twitter allows. You can specify which protocol to use as shown below. For more information on each of the authentication protocols, please check Twitter OAuth Help.

Instantiation with ACAccount:

// Instantiation using ACAccount
var swifter = Swifter(account: twitterAccount)

// Instantiation using Twitter's OAuth Consumer Key and secret
swifter = Swifter(consumerKey: TWITTER_CONSUMER_KEY, consumerSecret: TWITTER_CONSUMER_SECRET)

// Instantiation using App-Only authentication
swifter = Swifter(consumerKey: TWITTER_CONSUMER_KEY, consumerSecret: TWITTER_CONSUMER_SECRET, appOnly: true)

Example Requests

OAuth Authorization:

swifter.authorize(with: callbackURL, success: { accessToken, response in
  // ...
}, failure: { error in
  // ...
})

Get Home Timeline:

swifter.getHomeTimeline(count: 50, success: { json in
  // ...
}, failure: { error in
  // ...
})

ListTag, UserTag, and UsersTag

Certain Twitter API calls allows you to use either the user_id or screen_name to get user related objects (and list_id/slug for lists). Swifter offers a solution so that the user won't accidentally use the wrong method, and have nothing returned. For more information, check the SwifterTag.swift file.

swifter.getUserFollowersIDs(for: .id(userId), success: { json, prev, next in
    // alternatively, you can use .screenName(userName)
    // ...
}, failure: { error in
    // ...
})
swifter.getListSubscribers(for: .slug(listSlug, owner: .screenName(userName)), success: { json, prev, next in
    // alternatively, you can use .id(listId)
    // ...
}, failure: { error in
    // ...
})

Additionally, there is also .screenName(arrayOfUserNames) and .id(arrayOfIds) for methods that take arrays of screen names, or userIDs

Streaming API:

Important Note: Twitter has deprecated the Streaming API in favour of their new Accounts Activity API. You can find out more about migrating to the new API here. Twitter plans to remove the old streaming API on August 16, 2018, Swifter will remove the endpoints for it shortly after that.

swifter.streamRandomSampleTweets(progress: { status in
  // ...
}, stallWarnings: { code, message, percentFull in
  // ...
}, failure: { error in
  // ...
})

Status Update:

swifter.postTweet(status: "Hello, world.", success: { status in
  // ...
}, failure: { error in
  // ...
})

JSON Handling

To make accessing data returned by twitter requests simple, Swifter provides a class for representing JSON which you interact with similarly to a dictionary. The main advantage of using this instead of a Dictionary is that it works better with Swift's strict typing system and doesn't require you to constantly downcast accessed objects. It also removes the need for lots of optional chaining, making your code much cleaner and easier to read.

Here's an example of how you would access the text of the first element in list of statuses:

if let statusText = statuses[0]["text"].string {
    // ...
}

OAuth Consumer Tokens

In Twitter REST API v1.1, each client application must authenticate itself with consumer key and consumer secret tokens. You can request consumer tokens for your app on Twitter's dev website

Single Sign-On [Deprecated]

If you authorize with SSO, you should add URL-Scheme your Info.plist. REPLACE $(TWITTER_CONSUMER_KEY) TO YOUR CONSUMER KEY.

CFBundleURLTypes

	
		CFBundleTypeRole
		Editor
		CFBundleURLSchemes
		
			swifter-$(TWITTER_CONSUMER_KEY)
		
	

License

Swifter is licensed under the MIT License. See the LICENSE file for more information.

Comments
  • Streaming API Code sample

    Streaming API Code sample

    Hi,

    I am trying to get the streaming api working and the code that I have so far is as below.

    I have this method inside a singleton as suggested by @zntfdr here

    when I call this singleton method from my VC I do not see any responses at all in the print. Is there something I am missing here?

    I sometimes get this error message HTTP Status 420: Enhance Your Calm, Response: Exceeded connection limit for user which tells me that my connection is being established (or not). Do i need to put more tracker terms to see any significant streaming?

    func start(){
            
            let tracker = ["#PSX2017"]
            guard connection == nil else { return }
            print("ANN - friends list :", self.idList)
            if let sess = Twitter.sharedInstance().sessionStore.session() {
                let swifter = Swifter(consumerKey: myKey, consumerSecret: mySecret, oauthToken: sess.authToken, oauthTokenSecret: sess.authTokenSecret)
                print("Starting stream")
                self.connection = swifter.postTweetFilters(follow: nil, track: tracker, locations: nil, delimited: false, stallWarnings: true, filterLevel: nil, language: nil, progress: { (json) in
                    print(json)
                }, stallWarningHandler: { code, message, percentFull in
                    print("ANN: Stall error ", message!)
                }, failure: { error in
                    let err = error as! SwifterError
                    print("ANN: Error ", err.message)
                })
            }
        }
    
    opened by annjawn 21
  • [WIP] Integrate with the Swift 4 Decodable Protocol

    [WIP] Integrate with the Swift 4 Decodable Protocol

    Just wanted to throw this out there early to get some thoughts from the maintainers of the project (or anyone else, for that matter!).

    Basically, I'm not a fan of using the JSON enum for consuming the Twitter API. It's much better than having to cast from Any all over the place, but it's still pretty inelegant--especially compared to what you can get away with in weakly-typed languages like JavaScript.

    So, in parallel with a personal project of mine, I've been writing a wrapper API for Swifter, built on top of the Swift 4 Decodable protocol (actually originally built in private on top of the JSON type, then switched to Decodable). I intend to continue working on it no matter what, but I'm willing to go the extra mile and add support for all possible interactions with the API if it's decided that that would be within scope for the project.

    The main alternative I can think of to building this wrapper (other than doing nothing) is the following:

    • Provide generic methods for all API endpoints, which take generic success handlers, which take in arguments of type T where T: Decodable
    • Defer creation of wrapper types to the user

    Lastly, as of right now, all timeline and tweet methods have been implemented, and both demo apps have been updated to use the wrapper instead of the JSON API. Please feel free to ask questions, and give any feedback you have time to.

    Thanks!

    opened by ghost 19
  • Add support for getting raw and/or decoded data via Decodable protocol

    Add support for getting raw and/or decoded data via Decodable protocol

    As mentioned in #203, I found my previous solution to this problem #196 had a few issues in practice. I also would prefer not to have to maintain an independent fork if I don't have to. So I decided to take another go at it, and the solution I came up with this time is more flexible, much simpler and more ergonomic than the old one. It's also slightly different from what I proposed in the above issue.

    At the core of this pull request is a new type, RawSuccessHandler, which wraps a simple closure. A single parameter of this type is added to each endpoint method. The user can initialize it with one of three static methods to get either just the raw data, the raw data and a decoded value, or just a decoded value respectively--alternative name suggestions welcome:

    • public static func data(handler: @escaping (Data) -> Void) -> RawSuccessHandler
    • public static func dataAndDecoding<Entity: Decodable>(_ type: Entity.Type, handler: @escaping (Data, Entity?) -> Void) -> RawSuccessHandler
    • public static func decoding<Entity: Decodable>(_ type: Entity.Type, handler: @escaping (Entity) -> Void) -> RawSuccessHandler

    Behind the scenes, the closure wrapped by RawSuccessHandler itself accepts a Swifter.FailureHandler, which jsonRequest() takes advantage of to propagate decoding errors to the user-provided failure handler.

    Below are some code samples.

    Prints the home timeline in JSON form:

    swifter.getHomeTimeline(
        rawSuccess: .data { data in
            print(String(data: data, encoding: .utf8)!)
        }
    )
    

    Prints the home timeline in JSON form, prints a belittling message, then prints a decoding error:

    swifter.getHomeTimeline(
        rawSuccess: .dataAndDecoding(Int.self) { data, timeline in
            print(String(data: data, encoding: .utf8)!)
            print(timeline ?? "The home timeline isn't an integer, dummy!")
        },
        failure: { error in print(error) }
    )
    

    Prints the text of each tweet in the home timeline:

    struct Tweet: Decodable {
        let text: String
    }
    swifter.getHomeTimeline(
        rawSuccess: .decoding([Tweet].self) { tweets in
            for tweet in tweets {
                print(tweet.text)
            }
        }
    )
    
    opened by ghost 17
  • Repository Maintenance: call for contributors?

    Repository Maintenance: call for contributors?

    @mattdonnelly has been obviously busy in the past few months.

    This made the repo development suffer quite a bit.

    I honestly believe Swifter is an amazing framework and it would be a pity to see it die because of the lack of engagement of the repo owner.

    I'd like to propose a solution: much like https://github.com/danielgindi/ios-charts , I propose that @mattdonnelly elects some Swifter collaborators, which are contributors enabled to manage the repo almost like if it was theirs (closing issues, merging pull requests and the likes) in order to have a better/faster Swifter development.

    I'd say @meteochu is the most suitable for this position, but I leave to @mattdonnelly the final decision on the matter.

    Please, feel free to share any thoughts on the issue.

    opened by zntfdr 17
  • [ASK] Is there any way to Login with Installed Twitter app.?

    [ASK] Is there any way to Login with Installed Twitter app.?

    let swifter = Swifter(consumerKey: TWITTER_CONSUMER_KEY, consumerSecret: TWITTER_CONSUMER_SECRET,appOnly: true)

    this does not worked for me.

    I Wants to login user from Twitter app as well as Authentication from browser.

    From browser it's working.

    But it is not redirecting to Twitter app.

    opened by ZaidPathan 14
  • Compile errors

    Compile errors

    I followed your tutorial and added swifter xcode project to my project, but I am getting 151 compilation errors saying - swift compilation error in the SwifteriOS xcode project.

    I also tried to build swifteriOS project as it is and get the same compile errors.

    opened by srinivasmangipudi 14
  • [ASK] How do I save the session?

    [ASK] How do I save the session?

    I'm sorry if this is a stupid question, but I'm just learned about OAuth.

    I'm using your library for my school's project.

    how do I save the login session? so I don't need to relogin everytime I opened my application?

    thank you very much and sorry for my bad English.

    opened by iRILLLL 13
  • Get raw data (NSData/NSString) from JSON

    Get raw data (NSData/NSString) from JSON

    Hi,

    I want to store tweets (JSON) as an array of NSString or NSData (and not array of JSONValue). I got raw NSString for a tweet using the 'description' property. Now I'm having trouble converting it back to JSONValue format so it's parameters can be accessed.

    Is there a way to get raw JSON data and then convert it back to the Swifter's JSONValue format? Would be a big help.

    opened by bluekemon 12
  • Integer overflow in 32bit environment

    Integer overflow in 32bit environment

    Twitter's status id is now more than 2^32, so we cannot deal with the status id as Int with 32bit environment (such as iPhone4S).

    For example, the code doesn't work correctly with iPhone4S, due to integer overflow.

    let a:Int = 515496848048390145
    print(a) //prints -289275903 
    

    Therefore, I can't use func getStatusesShowWithID(id: Int, ...) to show https://twitter.com/gecko655/status/515496848048390145.

    I think Swifter should include the same method with String argument, like func getStatusesShowWithID(id: String, ...), or should change the method to func getStatusesShowWithID(id: Int64, ...).

    Is it correct?

    opened by gecko655 12
  • Move from CFURLCreateStringByAddingPercentEscapes to stringByAddingPercentEncodingWithAllowedCharacters

    Move from CFURLCreateStringByAddingPercentEscapes to stringByAddingPercentEncodingWithAllowedCharacters

    Hello. The warnings are:

    SwifterHTTPRequest.swift:170:31: 'init(request:delegate:)' was deprecated in iOS 9.0: Use NSURLSession (see NSURLSession.h)

    and

    String+Swifter.swift:57:22: 'CFURLCreateStringByAddingPercentEscapes' was deprecated in iOS 9.0: Use [NSString stringByAddingPercentEncodingWithAllowedCharacters:] instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent (since each URL component or subcomponent has different rules for what characters are valid).

    I realise these are tiny problems, but I haven't done any programming since May 2015, and I feel that I might cause more harm than good by trying to fix them myself just yet, so just flagging them up for others until my mind is more Swift 2.0 attuned. JBM.

    help wanted 
    opened by josephbeuysmum 11
  • All my requests break at SHA1DigestWithKey func

    All my requests break at SHA1DigestWithKey func

    Everytime I try to run this code, it breaks:

        var swifter = Swifter(consumerKey: "MY_API_KEY", consumerSecret: "MY_SECRET_KEY")
        swifter.getListsStatuesWithSlug("xxxxxx", ownerScreenName: "xxxxxx", sinceID: nil, maxID: nil, count: nil, includeEntities: false, includeRTs: false, success: {
    
            (lists: [JSONValue]?) in
    
    
            }, failure: {
    
                (error: NSError) in
    
                NSLog(" error => %@ ", error.userInfo)
    
        })
    

    It seems to do this with a few other swifter functions I have tried, none of them have worked so far.

    It breaks in the SHA1DigestWithKey func in the Swifter+String.swift class: CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), keyStr!, keyLen, str!, strLen, result)

    EXC_BAD_ACCESS(Code = 1, address = 0x14)

    opened by rgaliu 11
  • Deprecation notice

    Deprecation notice

    TL;DR This package will be deprecated in about one month. I'm not going to delete it for now but will no longer be maintaining it officially.


    Hi everyone,

    I created this project back when Swift was a very new language, as a way of learning it and sharing ideas on how to deal with APIs using it. Since then, it's grown a lot of in popularity and has been used by more people than I had ever imagined, which was been amazing to see.

    Unfortunately, I haven't had time to contribute actively to this project for several years. For a long time, I relied on other people who did a far better job at maintaining it than me. However, over time this repo has slipped out of date with Twitter's API and there's been less and less activity here.

    As a result, I've decided to officially deprecate this project in about one month from now. This means there will be no future changes to the repo, so on the off chance that anyone is still using this, you should seek out alternatives now. To deprecate it I will be will be taking the following steps:

    • Remove all collaborators with write access to prevent future changes
    • Update the README to indicate the project is no longer maintained and link here
    • Edit the README and repo description to indicate the project is no longer maintained
    • Add a deprecated GitHub topic

    My goal here is to prevent this from becoming yet another horror story about an inactive developer of a widely used library unintentionally distributing malware.

    I don't plan to the delete or transfer the repo to prevent it suddenly breaking people's code, but if anyone has other recommended courses of action or questions please leave them here.

    Massive thanks to everyone that has contributed and helped maintain this project thus far. Especially @meteochu and @zntfdr. I'll follow up here in a months time once I begin taking the steps to deprecate the project.

    opened by mattdonnelly 9
  • difference between default initializers?

    difference between default initializers?

    CleanShot 2021-11-19 at 22 37 17 if I understand correctly

    • Swifter initializer that takes the following consumerKey, consumerSecret, oauthToken, oauthToken secret is actually means request is sent through user's quota?
    • Swifter initializer that takes the following consumerKey, consumerSecret is actually means request is sent through developers quota?
    opened by pradeepb28 0
  • iOS login with twitter has error: CredStore - performQuery - Error copying matching creds

    iOS login with twitter has error: CredStore - performQuery - Error copying matching creds

      self.swifter = Swifter(consumerKey: Constants.TWITTER_CLIENT_KEY, consumerSecret: 
      self.swifter.authorize(withCallback: URL(string: "myApp://")!, presentingFrom: self, success: { accessToken, _ in
                print(accessToken)
            }, failure: { _ in
                print("ERROR: Trying to authorize")
            })
    

    I also added callback_url is "myApp://" on developer.twitter.com. But It's throw error: CredStore - performQuery - ``` Error copying matching creds. Error=-25300, query={ class = inet; "m_Limit" = "m_LimitAll"; ptcl = htps; "r_Attributes" = 1; sdmn = "https://api.twitter.com"; srvr = "api.twitter.com"; sync = syna; }

    opened by phungbuuquang 1
  • needs to update usage documentation for iOS13 & up

    needs to update usage documentation for iOS13 & up

    not sure if this repo is still active but the sample usage is not recommendable anymore since it's deprecated on ios13 so currently im trying to run ios15 so now I can't use the deprecated one.. I'm using the one who has a parameter of ASWebAuthenticationPresentationContextProviding but im still in progress and not sure if it'll work as expected.

    opened by ganico 1
Releases(2.5.0)
Owner
Matt Donnelly
Matt Donnelly
Telegram Bot Framework written in Swift 5.1 with SwiftNIO network framework

Telegrammer is open-source framework for Telegram Bots developers. It was built on top of Apple/SwiftNIO

Pataridze Givi 279 Jan 4, 2023
Google Directions API helper for iOS, written in Swift

PXGoogleDirections Google Directions API SDK for iOS, entirely written in Swift. ?? Features Supports all features from the Google Directions API as o

Romain L 268 Aug 18, 2022
Instagram API client written in Swift

SwiftInstagram is a wrapper for the Instagram API written in Swift. It allows you to authenticate users and request data from Instagram effortlessly.

Ander Goig 580 Nov 25, 2022
👤 Framework to Generate Random Users - An Unofficial Swift SDK for randomuser.me

RandomUserSwift is an easy to use Swift framework that provides the ability to generate random users and their accompanying data for your Swift applic

Wilson Ding 95 Sep 9, 2022
QuoteKit is a Swift framework to use the free APIs provided by Quotable created by Luke Peavey.

QuoteKit The QuoteKit is a Swift framework to use the free APIs provided by Quotable created by Luke Peavey.

Rudrank Riyam 17 Jun 23, 2022
The swiftest way to build iOS apps that connect to Salesforce

Build iOS apps fast on the Salesforce Platform with Swiftly Salesforce: Written entirely in Swift. Uses Swift's Combine framework to simplify complex,

Michael Epstein 131 Nov 23, 2022
CovidCertificate SDK for iOS

This is the Swiss implementation of the Electronic Health Certificates (EHN) Specification [1] used to verify the validity of Digital Covid Certificates. It is partly based on the reference implementation of EHN's ValidationCore

Swiss Admin 19 Apr 5, 2022
Backport of iOS 15 formatting api

This is a back-port of the .formatted API in Foundation that was introduced at WWDC '21 for iOS 15, macOS 12.0, tvOS 15.0, and watchOS 8.0.

Simon Salomons 9 Jul 22, 2022
App iOS correspondiente al proyecto twitimer.com de la comunidad MoureDev

⏳ Twitimer iOS Twitimer es una App gratuita para iOS y Android que se ha desarrollado para ayudar a usuarios de Twitch, pero sobre todo pensando en ge

Brais Moure 220 Jan 1, 2023
Unofficial iOS/macOS SDK for the Notion API.

NotionClient: a Notion SDK for iOS & macOS Unofficial Notion API SDK for iOS & macOS. This is an alpha version and still work in progress. TODO Featur

David De Bels 15 Aug 4, 2022
Desk360 Mobile Chat SDK for iOS

Desk360 Chat iOS SDK Desk360 Chat SDK provides simplicity and usability in one place. With this feature, you can provide live support to your customer

null 5 Sep 21, 2022
WANNA SDK enhances your iOS app with virtual try-on capabilities for shoes and watches

WANNA SDK enhances your iOS app with virtual try-on capabilities for shoes and watches. With this feature, your users will be able to see in real time how the selected product looks on them, just by pointing their smartphone camera at their feet or wrist.

Wannaby Inc. 18 Dec 2, 2022
Swift implementation of Github REST API v3

GitHubAPI Swift implementation of GitHub REST api v3. Library support Swift 4.2. Work is in progress. Currently supported: Issues API. Activity API(Fe

Serhii Londar 77 Jan 7, 2023
Swift Reddit API Wrapper

reddift reddift is Swift Reddit API Wrapper framework, and includes a browser is developed using the framework. Supports OAuth2(is not supported on tv

sonson 236 Dec 28, 2022
Swift client for Kubernetes

Table of contents Overview Compatibility Matrix Examples Usage Creating a client Configuring a client Client authentication Client DSL Advanced usage

Swiftkube 94 Dec 14, 2022
Instagram Private API Swift

SwiftyInsta Please notice SwiftyInsta may not be actively maintained at the moment of you reading this note. Refer to #244 for more info. Instagram of

Mahdi Makhdumi 218 Jan 5, 2023
SDK for creating Telegram Bots in Swift.

Chat • Changelog • Prerequisites • Getting started • Creating a new bot • Generating Xcode project • API overview • Debugging notes • Examples • Docum

Rapier 349 Dec 20, 2022
A Swift client for the OpenAI API.

OpenAI A Swift client for the OpenAI API. Requirements Swift 5.3+ An OpenAI API Key Example Usage Completions import OpenAI

Mattt 161 Dec 26, 2022
Swift Bot with Vapor for Telegram Bot Api

Telegram Vapor Bot Please support Swift Telegram Vapor Bot Lib development by giving a ⭐️ Telegram Bot based on Swift Vapor. Swift Server Side Communi

OleG. 104 Jan 6, 2023