Till today, the two most commonly used ways (without writing tediously long code) to integrate Twitter connect to your app is

The only problem I have with both these libraries is that, they are bloated. You end up adding at least 20 classes (more than 50 files in the first case and even more if you go with ShareKit).

Call me a person with Not Invented Here syndrome, but I hate adding third party code unless it offers considerable number of features that is critical to the app’s functionality. Today, when working on a code base, I deleted over 52 files that adds a functionality that allows a user to tweet “achievements” from within the app.

Deleting Twitter Sharing Code means getting rid of 52 files in your app

Deleting Twitter Sharing Code means getting rid of 52 files in your app

To me, this is a bloat. Same goes with ShareKit. You will use every single ShareKit supported social network only if you are writing a Google Reader (or any RSS reader) app or a Instapaper competitor. In most apps, Facebook and Twitter sharing is all that you need. 52 files for that, is a BLOAT.

Introducing RSOAuthEngine

RSOAuthEngine, in the words of the original author (Rodrigo Sieiro), is a ARC based OAuth engine for MKNetworkKit.

He originally posted his RSOAuthEngine on Github about a month ago. It is possibly the leanest implementation of Twitter oAuth I’ve ever seen. His sample code also contained a demo on how to use RSOAuthEngine with Twitter and with Instapaper.

The fork

Today, I forked his code and added a neat and important feature, iOS 5 Twitter Accounts support. I added a category class and a config file to his code and this is how it looks now.

RSOAuthEngine Files

RSOAuthEngine based Twitter Connect ~ 10 files

Just 10 files (3 classes and a category addition) that enables your app users to send tweets from the app.
Since MKNetworkKit provides operation freezing by default, users can send tweets even when they are offline. Those tweets are queued, frozen and sent when the connectivity is back again.

Using RSOAuthEngine

Step 1:
Clone the RSOAuthEngine fork from github. Add the RSOauthEngine code by dragging the directories RSOAuthEngine, Twitter and UIActionSheet+MKAdditions. If you are not using MKNetworkKit, you might have to add that too.

Step 2: Add the Twitter.framework and Account.Framework and weak link it. Weak linking ensures that your app doesn’t crash on devices running iOS 4 and below. If you are adding MKNetworkKit for the first time, you might want to add the related frameworks as well. See this blog post for details.

Step 3: Open RSTwitterConfigs.h and set the oAuth consumer key and consumer secret from twitter.
Fill in your callback URL. All these information should match exactly to the app you created on twitter’s website

Step 4: Initialize the RSTwitterEngine in your app delegate like this.

 self.twitterEngine = [[RSTwitterEngine alloc] initWithStatusChangedHandler:^(NSString *newStatus) {
    DLog(@"%@", newStatus);

Logging is purely optional.

Step 5: To send a tweet, set the presenting view controller of the twitter engine to the current view controller (where the “tweet/share button” is present) and call the sendTweet method.

ApplicationDelegate.twitterEngine.presentingViewController = self;
  [ApplicationDelegate.twitterEngine sendTweet:@"Hi there" withCompletionBlock:^(NSError *error) {

The magic behind the scenes

The fork I wrote automatically authenticates the user the first time you call the sendTweet:withCompletionBlock method. It presents a UIViewController with a UIWebView that shows the Twitter oAuth login page, extracts the credentials and saves it. The true magic is, on iOS 5+ devices, if the user has configured his twitter account on the Settings app, it shows the Twitter account authorization request and uses the built-in twitter account.

Accessing iOS Twitter Accounts

Accessing iOS Twitter Accounts


If the user has configured multiple twitter accounts, he sees

The code automatically shows this dialog when multiple iOS Twitter Accounts are available

The code automatically shows this dialog when multiple iOS Twitter Accounts are available

If there is exactly 1 account, that account is automatically used without further prompts.

The fallback

If there are no accounts configured or if the user is running iOS 4.3 or lower, the code falls back to traditional oAuth and shows a web view based authentication form.

Once the authentication workflow is done, the tweet is sent. Authentication information is saved and reused in subsequent calls to sendTweet:withCompletionBlock:.

The complete twitter sharing integration is done with just 3 classes. This, to me, is arguably the most leanest twitter sharing code in the Interwebs.

Fork it share it and do whatever you want. The code (both mine and Rodrigo’s) is licensed under MIT license.

On similar lines, I just finished a Facebook Sharing library


Follow me on Twitter