Saturday, February 11, 2012

Twitter integration with Iphone application

Please don’t use the TwitterAgent as I have received a number of comments that I does not work anymore since twitter moved away from the http api. I’ll be posting a new tutorial soon to show how to use their new API.

In this tutorial I am going to show you how to integrate Twitter in your iPhone app. I am going to have a short talk about Twitter Agent by Aman and how to use it to integrate twittering game scores out of a small example game called iDice.

If you are already reading this you probably are looking into having an easy way to integrate some twittering in your app- fact is Twitter have quite good API (even 2 or 3 APIs if we want to be exact) and around the web there are some quite good Twitter engines already written in Objective-C. However today I’m gonna look into definitely the easiest way to provide twittering within your iPhone app.

I mean – 1 line of Objective-C code – how better can you do ?

Twitter Agent

Twitter Agent is one of those all-in-one libraries which have a single purpose and do it well, while still leave you the possibility to customize what you want. You can head straight away to google code here (only SVN access to the source code at the moment) or to this blog article to download the code as zip file. Be sure to return here to see the cool example I prepared ;)

First of all, how you go about twittering in 1 line of Objective-C? Look at this example:

[[TwitterAgent defaultAgent] twit ];

TwitterAgent has a static instance, which you can access straight away and a method “twit” which you can call: this code will show the user a twitter login window and after a successful login will present him with a textfield (and a char counter) where he/she can enter his tweet and send it into the wild. The twit method is really nicely wrapped, but there’s more to TwitterAgent than this simple call.

[[TwitterAgent defaultAgent] twit:@"Touch code magazine is great!" ];

This way the user is given a pre- filled text message to tweet, and if you also want to have a URL already shortened :

[TwitterAgent defaultAgent] twit:@"Touch code magazine is great!" withLink:@"http://www.touch-code-magazine.com" makeTiny:YES];

I really think Twitter integration does not get any simpler than that. If you want to change something in the looks of the dialogues or the functionality – just dig into the few very simple classes, it won’t take you much to figure your way.

iDice – a socially involved iPhone game

I’m going to put together a simple game which will give the user the possibility to tweet his game results. First I’m importing the TwitterAgent files and BusyAgent (simple class to dim the screen and let the user know there’s an action in progress) into my XCode project. I’ll just copy over the source files from the zip files I downloaded:

After that I craft fast in Interface Builder the interface of my game: 2 dices which will be the player and computer and 2 buttons – one to roll the dices again and on to tweet about my game result. (Won’t get in much details how to put together a UIViewController here)

In the action of the “Roll dices” button I put a simple code to generate 2 random numbers and show them in my 2 colorful UILabels (those are the virtual dices):

-(IBAction)rollDice {  playerScore = arc4random() % 6 + 1;  lblPlayer.text = [NSString stringWithFormat:@"%i", playerScore];  computerScore = arc4random() % 6 + 1;  lblComputer.text = [NSString stringWithFormat:@"%i", computerScore]; }

I include the TwitterAgent in my iDiceViewController.h:

#import "TwitterAgent.h"

Now comes the real meat of the tutorial, if the user is happy with his results (say he got a 6 and the computer 5) he can tap the “Tweet my results” button and show off in Twitter. Here is the complete code of the button’s action:

-(IBAction)tweetResults {  NSString* message;  if (playerScore > computerScore) {   message = [NSString stringWithFormat:@"I won at iDice %i to %i !", playerScore, computerScore];  } else if (playerScore < computerScore) {   message = [NSString stringWithFormat:@"I lost at iDice %i to %i", playerScore, computerScore];  } else {   message = [NSString stringWithFormat:@"A damn tie at iDice %i to %i ...", playerScore, computerScore];  }    [[TwitterAgent defaultAgent] twit:message withLink:@"http://www.touch-code-magazine.com" makeTiny:YES]; }

As a little extra I added a URL in the message – should have been the iDice app web site, but since iDice still does not have one / :) / I did put the Touch Code Magazine URL address.

And now – tap “Roll dices” and tweet results:

Now that tweeting is fully integrated in my iDice game and I thank Aman for putting in together I go on to testing and security. One thing I won’t like is the fact the user can edit the message: if actually looses he can edit the message as if he had won!

As I said TwitterAgent is quite simple and flexible, I’ll tamper a bit with the code to make the Twitter message editing disabled. In TwitterAgent.m on line 25 there’s the code which initializes the TextView field for the text message. Feels like a good place to change a property of this field. The changed code looks like this:

 txtMessage = [[UITextView alloc] initWithFrame:CGRectMake(15, 80, 250, 60)];  txtMessage.editable = NO;

Easy – now the user must tweet his real results, if he does not want to – he can tap Cancel and forfeit.

What’s next for TwitterAgent integration?

There many other easy tweaks one could do to customize the twitter agent to his needs – first thing coming in my mind is to actually have a delegate which gets notified when the message is sent, localizations, Twitpic integrations, etc.

Aman is also inviting others to join on Google code and bring more features to TwitterAgent – if you feel like you can contribute do go ahead!

Now one side note on security, the TwitterAgent uses plain http to make calls to Twitter, if you are a security freak that might alarm you, but if you are a casual score submitting game author, I guess it’s just fine.

That’s a wrap up for today

You can download the iDice game source code here. If you liked this article please ping me on Twitter or leave a comment.

TwitterAgent’s author’s site – here.

Your friend prasad

No comments:

Post a Comment