Matt Legend Gemmell Modesty is Lying

Mac OS X Cocoa and iPhone Development Services available at Instinctive Code.
Favorites icon
Favorites for iPhone
Speed-dial with style.
Mac OS X Cocoa and iPhone Developer for hire
Sheercore Comics - Geeks with Swords!

Other Pages

Categories

Posted
22 February 2008 @ 12pm

Categories
Development, Source

Tags
, , , , , , ,

MGTwitterEngine – Twitter from Cocoa

Ever wanted to integrate Twitter support into your Cocoa app? I know I did. Enter MGTwitterEngine, available as always from my Cocoa Source Code page. (Oh, and I’m “mattgemmell” on Twitter, by the way).

MGTwitterEngine is an Objective-C class which lets you integrate Twitter support into your Cocoa application, by making use of the Twitter API. The entire API is covered, and appropriate data is returned as simple native Cocoa objects (NSArrays, NSDictionarys, NSStrings, NSDates and so on), for very easy integration into your own application. MGTwitterEngine is designed for Leopard, but should be just fine on Tiger too.

Give it a shot and see what you think – you can grab the code from my public subversion repository at svn.cocoasourcecode.com. Maybe you could make the next Twitterrific?

Update: Since I made this post, MGTwitterEngine has been updated several times, so be sure to read on for more details. It will now build for iPhone too (using the official SDK), and an article by Tim Burks on using MGTwitterEngine via Nu.

Creating a TwitterEngine is as easy as instantiating it, setting your Twitter username and password, and you’re done:

MGTwitterEngine *twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
[twitterEngine setUsername:@"username" password:@"password"];

// Get updates from people the authenticated user follows.
NSString *connectionID = [twitterEngine getFollowedTimelineFor:nil since:nil startingAtPage:0];

You just implement a few delegate methods (for returned Twitter statuses, user information, and direct messages), and MGTwitterEngine does all the rest. It supports regular or secure connections, custom Twitter client information (so updates sent from your app can show as “from MyCoolApp” on the Twitter website), and handles all the boring stuff like network connections and XML parsing.

(Note: XMPP and OAuth support coming later, when Twitter’s implementation stabilises!)


Update 1: Now includes a Read Me giving some information on how to use the class and what types of data it returns.

Update 2: MGTwitterEngine can now be built for iPhone, using the official SDK from Apple.

Update 3: Source code has been updated, thanks to feedback from Craig Hockenberry (of Twitterrific fame).

  • We now specify login credentials encoded in the URL instead of in the request headers. This removes the requirement for linking the libcrypto framework or including the NSData+Base64Extensions category files.
  • Secure (HTTPS) connections are now on by default.
  • We now remove cookies after setting a new username/password, to ensure that the next request uses the correct credentials.
  • Added method for sending an update in reply to a specific other update. Support for this on Twitter’s side hasn’t gone live yet, but when it does this should just work transparently.
  • Fixed a small memory leak.

Thanks to Craig for the benefit of his experience working on Twitterrific.

Update 4: Code updated once again; get it from the svn repository mentioned previously. Each tweet or direct message returned by MGTwitterEngine now has an extra value in the NSDictionary, specifying what type of request was sent to Twitter and produced that tweet/message. This is very handy if you need to quickly know that, say, a tweet came from the list of replies instead of from the main tweets timeline, so that you can colour replies differently from regular tweets (as Twitterrific does).

This useful feature was implemented due to a request from Craig Hockenberry; thanks to Craig for the suggestion.


70 Comments

Mathieu Tozer
22 February 2008 @ 4pm

Thank you! I already have an app idea :)


Peter Hosey
22 February 2008 @ 7pm

> custom Twitter client information

Perhaps it could auto-detect this from [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey]?


Mathieu
22 February 2008 @ 7pm

Congrats Matt! It’s going to be very useful for a lot of us!


robby valles
22 February 2008 @ 8pm

nice!! good job :)


Jon Crosby
22 February 2008 @ 9pm

Possibly of interest if you go the OAuth route: http://code.google.com/p/oauthconsumer/wiki/UsingOAuthConsumer


Matt Legend Gemmell
24 February 2008 @ 4pm

HI all,
Cheers for the positive feedback – hope you’ll find the code useful. :)

Peter, my feeling was that that kind of thing was better implemented in the client which uses MGTwitterEngine, rather than in the class itself – I deliberately tried to keep the API as implementation-agnostic as possible.

Jon, thanks for that. I actually already had your code bookmarked for future use in that regard. ;)


Raphael Bartolomé
5 March 2008 @ 10am

Hi Matt,
great API!! I have implement it in my Teatimer.
It works like magic, awesome …

Thank you!


Tim Burks
5 March 2008 @ 2pm

Hi Matt,

You might enjoy scripting MGTwitterEngine with Nu:
http://blog.neontology.com/posts/2008/03/04/twitter-and-nu


Lachie Cox
10 April 2008 @ 11pm

Hey this is awesome!

I’m having trouble getting the User timeline, using:
[twitterEngine getUserTimelineFor:nil since:nil count:0];
creates the url:
https://lachie:XXXX@twitter.com/statuses/user_timeline.xml

This works when I plug it into curl, but using Twitter Engine I get a 401 Unauthorized

any ideas? I’m about to get wireshark onto it…
thanks again for the code
Lachie


Lachie Cox
10 April 2008 @ 11pm

Examining the http dialogue in wireshark shows that the credentials are being dropped from the url
http://lachie:XXXX@twitter.com/statuses/user_timeline.xml
User-Agent is CFNetwork/221.5
I’ll try to patch it if I can :)

I’m on 10.5.2 with the xcode etc from the first iphone beta (though not compiling for the iphone).


Matt Legend Gemmell
11 April 2008 @ 8am

Hi Lachie,

Looks like it’s an issue on Twitter’s side. MGTwitterEngine does send the auth details as normal, but there’s something weird going on with that particular URL. You can easily work around it by doing this instead:

[twitterEngine getUserTimelineFor:username since:nil count:0];

(i.e. just pass the authenticated user’s username as the first parameter.)


Robert McGovern
30 May 2008 @ 8am

Seems that line: [twitterEngine getUserTimelineFor:nil since:nil count:0]; is still causing problems with Twitter.

I had just been trying the sample app and wondering why it was failing then saw Lachie’s comment and your reply.


scaramangado
11 July 2008 @ 2pm

Why don’t you create a framework? I think that would by a good addition to the sourcecode.


[...] definitely also see Matt Gemmell’s MGTwitterEngine. 01 Oct 2008 08:38 am [...]


News » XML parsing on iPhone
2 October 2008 @ 3pm

[...] And definitely also see Matt Gemmell’s MGTwitterEngine. [...]


Oriol Ferrer Mesià
31 January 2009 @ 5am

Hey where does that yajl stuff come from? It’s not in your svn source tree and no note about it on the readme….

#include


Matt Legend Gemmell
31 January 2009 @ 9am

Hi all,

We’re currently overhauling MGTwitterEngine, hence the YAJL stuff etc. In the meantime, please use the last stable version, 1.0.8, which is here: http://svn.cocoasourcecode.com/MGTwitterEngine-1.0.8/


User
7 February 2009 @ 10pm

Woah, just checked out the yajl branch by accident. Looks fantastic, any word on when that’ll be done?


Dominic
11 February 2009 @ 6pm

How can i easily just set a status from a text box and a button? It’s simply all I need to start off.


[...] MGTwitterEngine by Matt Gemmell.  Integrate Twitter support into your Cocoa app. [...]


Mike Rundle
15 February 2009 @ 5am

Dominic, look at the methods in MGTwitterEngine.h to see the specific call you need to make. Once you have instantiated an MGTwitterEngine object and logged in with credentials, it’s a 1-liner to post a new status update to Twitter.


Oriol Ferrer Mesià
15 February 2009 @ 10pm

Thanks for sharing this! Really appreciate it


Joseph McLaughlin
7 March 2009 @ 3pm

Thanks for your work on this! It’s an awesome library/wrapper around the REST calls.


GregM
10 March 2009 @ 11pm

Thanks very much for providing this!

I don’t see a method for retrieving the timeline for a password-protected Twitter account. Am I missing something, or is that not currently supported?

Thanks again.


Mark
25 March 2009 @ 6am

I had some trouble checking out the svn repository. Kept getting an error on checkout, so I just downloaded all the files using wget. Anyone else able to checkout? I was using this command:
svn co http://svn.cocoasourcecode.com/MGTwitterEngine-1.0.8/ ./

Thanks for the hard work everyone has put in on this. It worked very well and I was able to easily follow the readme. Got it working on the first try!


Vic
2 April 2009 @ 2am

I am having trouble compiling the program on 2.2.1 SDK. I created a new app for iPhone, added the MGTwitterEngine files etc. I get the following error:

.objc_class_name_MGTwitterEngine”, referenced from:
literal-pointer@_OBJC@_cls_refs@MGTwitterEngine in TestAppDelegate.0
symbol(s) not found
collect2: Id returned 1 exit status

I did link in /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/usr/lib/libxml2.dylib and added “$SDKROOT/usr/include/libxml2″ in Header Search Paths.


Vic
3 April 2009 @ 12am

Resolved. Apparently, I had missed some files when I added them to my project. After redownloading the source and recreating the project, it worked. Thank you.


Michael Pelz-Sherman
10 April 2009 @ 10pm

That svn command worked fine for me.


sam
12 April 2009 @ 6pm

Awesome code; works great on the simulator. Thanks so much.

I’m getting a warning when I build for the device, regarding this function:

[NSDate dateWithNaturalLanguageString:...];

in MGTwitterXMLParser.m.

Is anyone else seeing this? It seems this method isn’t a part of the SDK.. Works on the simulator for some reason.

sam


Cassio
22 April 2009 @ 5pm

Dear Matt, maybe I’m missing something, but I’m trying to work with your framework but after the login, I can’t see the twitters. Where are they? How can I “print it” on display? Is there any additional code you can provide me to see how to use it?
Thanks and regards,


Chris Kimpton
23 April 2009 @ 6am

Hi Matt

Thanks very much for the library – got it running very quickly.

On the clientname/token stuff – if I don’t set the token, messages come as sent by MGTwitterEngine, but if I set the token to either of the consumer key or secret values I have from Twitter for my app, it just says from ‘web’.

On my twitter app page, it refers to oauth – I am guessing oauth is now required for this to work…

I am using the 1.0.8 version of MGTwitterEngine.

Thanks,
Chris


Fish
23 April 2009 @ 5pm

What is the eta for oauth support?


Chris
24 April 2009 @ 3am

I keep getting YAJL build errors, any idea where I can get those files and where I put them?


alex
27 April 2009 @ 6am

an error occurs while trying to compile the app. it says can’t find yaji/yaji_parse.h


Chris Kimpton
28 April 2009 @ 7am

FYI I have made some fairly small changes to MGTE 1.0.8 to use Jon’s OAuth library. Not done exhaustive testing but can get timeline and do status update with the correct source being shown. I can share it on github or wherever you suggest.

Chris


Chris Kimpton
3 May 2009 @ 10am

I have put my changes up on github, if anyone is interested.

The MGTwitter changes in here – https://github.com/kimptoc/MGTwitterEngine-1.0.8-OAuth/tree

A simple wrapper class to do the OAuth setup and pass it to MGTwitterEnginer – https://github.com/kimptoc/MGTwitterEngine-1.0.8-OAuth-MyTwitter/tree

README file is here – http://github.com/kimptoc/MGTwitterEngine-1.0.8-OAuth-MyTwitter/blob/e0f337ec8aa20fe0883eb4c9ed7cc39d7efab859/README


Michael Pelz-Sherman
3 May 2009 @ 7pm

Hi,

Are Chris Kimpton’s OAuth changes needed to get Twitter to recognize and display the name of my client?

I’ve registered my app with Twitter and set the clientName and token, but the tweets from my client still say “from web”.


Michael Pelz-Sherman
4 May 2009 @ 11am

The answer to my question above is “yes”.

I’m having some trouble implementing Chris’ changes. How’s the OAuth support coming along?


moe
11 May 2009 @ 7am

Hey Matt great work! And Chris thanks for your work!

Beeing quite inexperienced in all those code stuff i think i’m not able to integrate oauth in my project but want to see “from myappblabla”. Hmmm i know everything is explained well but i dont understand it…

matt are you going to integrate it in your code in the next time? or do i have to integrate chris hacks? ^^


Andy Black
12 May 2009 @ 1am

Hi,

I recently found MGTwitter Engine and have been playing around with and have a question. When exactly are the results returned to the delegate methods you implement in your own code?

In my delegate implementation for – (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)identifier
I put statuses into an array of my own to use later but it never gets populated.

Thanks,
Andy


Shaikh Sonny Aman
15 May 2009 @ 12pm

Hi,

I have to create an iphone app (say “testapp”) having feature to tweet and it should show “sdddf” — sent from testapp on the twitter page.

Can anyone plz tell if the user need to approve the application first? is it possible that the user will only give his/her twitter username/password and the testapplication will tweet without registration?

Has anyone implemented oauth and mgtwitter? Is there any opensource project?

While compiling I am having the following error:

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/usr/include/fenv.h:41:6: error: #error This fenv header and set of APIs is intended for the ARM (with VFP) architecture only.

anyidea?

Thanks,
Aman


Michael Pelz-Sherman
17 May 2009 @ 8pm

Folks – the only way to get your tweets to show up as from your app is to use OAuth.

I did manage to get Chris’ code to work, but it took a bit of effort.

Pssst: hey Matt, how ’bout putting this code on GitHub or google code & letting others contribute? :-)


Erik
21 May 2009 @ 6pm

Can you please post the YAJL framework.
Thanks


Bijan
22 May 2009 @ 1pm

Hi,

Matt, thank you for sharing this wonderful library.

By the way, does anyone know if MGTwitterEngine is thread-safe?

Thanks in advance.


Robb
24 May 2009 @ 6am

Thanks Matt and Chris for your contributions. I’m having a heck of a time trying to get OAuth working with Chris’s code in an iPhone proj. All sorts of errors no matter what I do. Not sure what I’m doing wrong. Can anyone provide tips on this?


Robb
24 May 2009 @ 10pm

I was finally able to implement Chris’ code for the iPhone. Only problem is, the user has to go to the authorization website every time they use the app. I don’t want this. Does anyone know how to avoid this?


Bryan
26 May 2009 @ 10pm

@Michael – Did you get the OAuth to work as a small test app? can you post your work somewhere. I’m currently trying to get this to work so I can change my code on a different project but currently not having luck. Currently stuck at the auth section (also says that sendUpdate is not implemented in the MyTwitter file.
Thanks


Alex
27 May 2009 @ 12am

How do I turn off Yajl? or where is it?

It says it’s in the MGTwitterEngine but I can’t se it in the directory.
Can’t build a simple test, Help.


Twitter Search
27 May 2009 @ 1am

thats great that you are talking about the twitter api,a good example of searching with the twitter api is on twiogle.com because you can search on twitter and google at the same time.


Mike Huntington
27 May 2009 @ 5am

I’m having the same problem as Andy Black…. my responses come back perfectly … but when I try to alloc a new array with the response data … it always is empty .. infact it seems the Array is being alloc’ed before the response result array is even filled.


Alex
27 May 2009 @ 5pm

Sorry about previous post. I was not using MGTwitterEngine-1.0.8 but MGTwitterEngine.
Anybody successfully compiled MGTwitterEngine-1.0.8 on iphone sdk 2.2.1?
I am getting some errors that I don’t know what to do with. I managed to compile on
OSX without any warnings or problems but on the Iphone I am getting :

Line Location Tool:0: collect2: ld returned 1 exit status
Line Location Tool:0: symbol(s) not found
Line Location Tool:0: -[MGTwitterLibXMLParser initWithXML:delegate:connectionIdentifier:requestType:responseType:URL:] in MGTwitterLibXMLParser.o
Line Location Tool:0: “_xmlReaderForMemory”, referenced from:
Line Location Tool:0: -[MGTwitterLibXMLParser _nodeValue] in MGTwitterLibXMLParser.o

… But it’s too long to post.

Any ideas?

By the way, Good Job Matt!!! and thanks for letting us use your code.

Best.


Elizabeth
2 June 2009 @ 5am

Wonderful code Matt!

One question: is there any way to set up the app to upload pictures to twitpic in addition to the text or is that a totally separate matter?


Garrett
5 June 2009 @ 10pm

I hope I’m reading things incorrectly, but it looks like the http://www.twitpocalypse.com/ is going to affect MGTwitterEngine…It looks like all the updateID’s are ints (a max of 2,147,483,647 on the iPhone)… it’s probably a good idea to change them unsigned long longs or at the very least uints…


Slavik
7 June 2009 @ 7am

Great work. Think it’s more useful to update the code with iPhone compiled version. Because it’s not easy get 1.0.8 with 2.2.1 or 3.0


Pete S
9 June 2009 @ 9pm

Thanks for the superb API (a donation is coming shortly).

I have it working just fine on the iPhone, I just followed the instructions (I did get the latest from SVN and just removed all the yajl files.

Just like everyone else, I would love to have it display my app name on the Tweets, but I’ll be shipping soon, so it will just say ‘from the web’… Oh well. Beggars can’t be choosy :)

Thanks


[...] MGTwitterEngine – Twitter from Cocoa [...]


MS
12 June 2009 @ 7pm

Thanks for putting this together. Will help a lot of people. Good work!


Stephen
13 June 2009 @ 1am

Getting a “YAJL error 3″ on Twitterfic for iPhone at the moment, any ideas what’s going on? Thanks


Dominic
13 June 2009 @ 10pm

Hey, I’ve realised that the Twitpocolypse has affected MGTwitterEngine, is there a way of getting it to work now it’s happened?


MattLook
16 June 2009 @ 2pm

Did not really understand how to have “from MyCoolApp” instead of MGTwitterEngine. I registered my app and have Consumer key, Consumer secret and Request token URL, Access token URL, Authorize URL (the URL seem really general URL. Not specific to my app). Any idea or links that could help me?Thanks folks!


[...] the Twitter plug-in to use the latest version of the MGTwitterEngine, which fixes issues related to the Twitpocalypse. Simon wasn’t affected, as it always fetches [...]


Devang
19 June 2009 @ 10am

Hi,
Thanks Matt for the wonderful work. I want to change the profile image of the user using your library. I am trying to do it since last three days. But not getting success.My application crashes while creating the request in which it tries to convert nsdata to string and pass it the request. Any help would be surely appreciated.
Thanks


Chris Kimpton
20 June 2009 @ 11am

Hi

just a heads up – seems like Twitter api is now using 1.0a which needs an additional pin/verifier sent at the last oauth step :( .

Maybe we should set up a google group for this discussion?

Regards , Chris


Chris Kimpton
21 June 2009 @ 7pm

Hi,

Further to my comment yesterday, the changes I had to make was a small patch to OAuth – see http://code.google.com/p/oauthconsumer/wiki/UsingOAuthConsumer

And then in the interaction after the user closes the browser, you need to ask them for a pin (yuck :( ) and then pass this to Twitter when asking for the access token, as the oauth_verifier.

I will update my hacked code with my changes – hope it helps.

I must look at the HEAD code – it must have OAuth support in it… or what is Adium doing…

Regards,
Chris

PS How about MGTwitterEngine-User as a google user group?


Isaiah
25 June 2009 @ 7am

Hi,

I borrowed heavily from Chris Kimpton’s awesome work and built a complete project out of it. Included all of the frameworks etc.

This means that you can click the download button. Open the project. Push run.

I did change one thing. I subclass the MGTwitterEngine instead of modifying it. That way we don’t have to fork MGTwitterEngine and can stay current with Matt’s work and others’ contributions.

My solution is to include an internal web-view into the project to display the “Accept” page. This basically replaces the “username/password” view that would normally be in a project.

I’ve opened all the source, and put it on github. Please feel free to use it, modify it, or comment on it. Thanks again to Chris and Matt for making this possible.

http://github.com/yourhead/OAuth_ObjC_Test_App/tree/master

Thanks,
Isaiah
YourHead Software


isaiah
28 June 2009 @ 6am

Oh, and I thought I should mention that I put together a pre-compiled version too. Just so that you can see what it does without having to register OAuth credentials with Twitter. Here’s the link:
http://thurly.net//0w7


perplexed
28 June 2009 @ 11pm

I would also love to know how to change “from MGTwitterEngine” to “from MyCoolApp” when a tweet is posted.
When you register an app with Twitter, it gives you a Consumer Key, Consumer Secret and 3 very general request, access, and authorize URLs. Where do you enter this information in MGTwitterEngine to customize the app signature?


MattLook
30 June 2009 @ 1pm

Hi folks!
I am still trying to have a client token for my app. I have a consumer key and consumer secret. It seems that once you have a client token for your app, twitter does not change it. I used Chris’ code but I still do not have my client token. Any link where it is explained (I am new to iPhone programming and oauth is complex for me to understand)?
Thanks a lot!


isaiah
2 July 2009 @ 5pm

@perplexed -
The reason that the “from MGTwitterEngine” is shown is that your OAuth is not quite working, or that you’re still using basic auth.
To get your client info to show you MUST use OAuth. Check the Twitter API pages for more details — but they’re pretty much dead-set on it.


isaiah
2 July 2009 @ 5pm

@MattLook
The consumer key and consumer secret that you were given by Twitter when you registered your app need to be placed into the code that you’re using from Chris. In Chris’ OAuth wrapper class (posted about above) you should look for the code that looks like this:
#define kOAuthConsumerKey @”"
#define kOAuthConsumerSecret @”"
That’s where your keys go.
Hope this helps, you can ask me more via twitter: @isaiah

Isaiah


Leave a Comment

Irate about User Interface Skinnable Cocoa UI with WebKit and CSS