Hi this is Marin - the author of Touch Code Magazine, I hope you are enjoying my tutorials and articles. Also if you need a bright iPhone developer overseas contact me - I do contract work. Here's my LinkedIn profile
Easiest way to integrate Twitter in your iPhone app

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.

Marin Todorov

The post was originally published on the following URL: http://www.touch-code-magazine.com/easiest-way-to-integrate-twitter-in-your-iphone-app/

 

  ·

 


Marin Todorov

is an independent iOS developer and publisher. He's got more than 18 years of experience in a dozen of languages and platforms. This is his writing project.
» Contact    » Add Marin on Google+

  1. [...] This post was mentioned on Twitter by icanzilb, icanzilb. icanzilb said: #touchcodemagazine: Easiest way to integrate Twitter in your iPhone app http://bit.ly/diTNGo [...]

  2. physical therapist on Wednesday 28, 2010

    Keep posting stuff like this i really like it

  3. Aman on Wednesday 28, 2010

    Nice tutorial, surely it will come into help of many.

    Thanks for using TwitterAgent and inviting others to join!

  4. chicknstu on Wednesday 28, 2010

    That’s absolutely brilliant!

    One quick question. When you post to twitter it says ‘via API’ under the tweet. Where does it get this from and is there any way to change it? e.g., to the name of your app?

  5. Marin on Wednesday 28, 2010

    Hi chichnstu, that’s a problem indeed … to have the tweet saying “via myappname” you must use the new OAuth way of authorization which is still kind of tricky to get working. Twitter Agent is pretty simple and uses the Basic authorization which does not provide you the tweet signature :)

  6. Wordpress Themes on Wednesday 28, 2010

    Genial post and this fill someone in on helped me alot in my college assignement. Thank you for your information.

  7. WP Themes on Wednesday 28, 2010

    Good fill someone in on and this post helped me alot in my college assignement. Thank you as your information.

  8. anil nigam on Wednesday 28, 2010

    hi. this code is not working . plz help me

  9. Marin on Wednesday 28, 2010

    Hi Anil,
    twitter recently changed their API, so you’ll have to look for an oAuth solution.

    best, Marin

  10. Muhammad Faisal Aleem on Wednesday 28, 2010

    If the device is in the landscape mode the twitter alert box that appear is not in correct order
    otherwise its a good and easiest method of implementing twitter but if the app runs in landscape mode it is of no use

  11. Marin on Wednesday 28, 2010

    Thanks for the comment Muhammad, I didn’t know about the landscape problem! Btw I’m not so sure if twitter still supports the http authentication at all

  12. henk on Wednesday 28, 2010

    it conforms that the tweet is posted but I can see on my twitter :(

  13. Marin on Wednesday 28, 2010

    this one should not work anymore should takw the article down amd make a new one

  14. Rinku on Wednesday 28, 2010

    its good example but can u plz tell me y it is not working in my app.i m fresher i have done registration n get consumer key n secret i have send mail at api@twitter.com but not got any replies..now i m trying but not working what should i do?

  15. Rinku on Wednesday 28, 2010

    its great tutorial i have used this..all is working fine,tweets are poatedd from my app but not showing in my twitter account why?can u plz give me soln..