Monday, 19 December 2011

Beginning Twitter integration iOS 5: Part 1

iOS 5 came with a bang and with tons of surprises. 1500  new API, sounds very interesting.

One of the good thing is that  iOS 5 ships with the Twitter framework. So it means no hectic code, oauth implementation by the developer. All of it is done and taken care by this framework.
And the best part is that it supports multiple accounts.

So Today we gonna sneak peak in this framework and will implement a simple twitter Application that will be sending  your tweet.

Get Started
As we are using new feature of iOS 5, so i am assuming that you have Xcode 4+.

> Fire up your Xcode and name the project as LetsTweet.
Make sure that ARC(Automatic reference counting) is enabled.

>Select the Single View Application.

Adding Twitter Framework

This is accomplished by clicking on your project in the Navigator sidebar, selecting your project’s target, and going to Build Phases. This should pull up a table with a few items. Click the arrow next to Link Binary With Libraries to expand the options and click the + button to add a new library. Find the library named Twitter.framework in the popup menu and select add button.
In the end it will be like:



As we are only going to implement the very basic functionality i.e to tweet, So our view controller will be simple too.

> Now go to your ViewController.xib, drag a UIButton to the View, and edit the text on it to be Tweet.



> Click on the file ViewController.h, here we are going to declare a function that will handle the tweet button click.

> In your ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    
    
}

-(IBAction)letsTweet: (id) sender ;

@end



> Now connect the function "letsTweet" to Sent events (Touch up) of tweet button.


> Lets program the ViewController.m file.

For using the Twitter framework you have to import it to your Class.

so include this 

 #import "Twitter/Twitter.h"

So in your .m file implement the  -(IBAction)letsTweet:(id)sender function as below.

-(IBAction)letsTweet:(id)sender {
    //Create the tweet sheet
    TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
    
    //Customize the tweet sheet here
    //Add a tweet message,
    [tweetSheet setInitialText:@"This is iOS Muncher sending first tweet. :)"];    
       
    
    //Set a blocking handler for the tweet sheet
    tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result)
    {
        [self dismissModalViewControllerAnimated:YES];
    };
    
    //Show the tweet sheet!
    [self presentModalViewController:tweetSheet animated:YES];
    
}


Lets understand the code:

1.  TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController allocinit];

This allocates a new tweetSheet for us, this is the in-build ViewController that will be seen when you tap on tweet button.

2.   [tweetSheet setInitialText:@"This is iOS Muncher sending first tweet. :)"];

We can set initial text for the tweet, you can edit it later also when you are on the tweetSheet.

3.   tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result)
    {
        [self dismissModalViewControllerAnimated:YES];
    };

Here we have to set a blocking handler for the result. This is to ensure that we don’t mess up the rest of the app while tweeting until we dismiss the modal view (that is our tweet sheet)

4. [self presentModalViewController:tweetSheet animated:YES];

This shows up your tweet sheet.


Running the Application:

Hit Build and Run button, you will the see the app running in your simulator.

Tap on tweet Button. If you see this screen as below, dont panic.


All iOS 5 devices have a feature in their setting page, where user can set their twitter account.
You have to set the account only one time, and it will not ask you again and again to authorize while posting tweets. cool ha..!!

> Just click on settings and add your twitter account.


>After adding your account. Launch the App again and just tweet.




So you are ready to send your own tweet from your own App.

There are lots customization that you can do with this framework, like you can tweet pictures, url, can see your friends tweet, search something on twitter,get tweets from particular accounts and lot more.

Well this was just a basic, in the coming post we will fully customize our LetsTweet Application

Stay Tuned.. :)





No comments:

Post a Comment

Pages