Update: If you’re looking for information on getting MGTwitterEngine working with xAuth, please read this tutorial!
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 the public repository on github. 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 there’s also 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 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.
Hi Matt,
I wasn’t sure if this was the right way to ask this question or where I should address it.
I’m using the MGTwitter Engine and it’s working great!
I have noticed though that even though the returned json for some of the requests contains all the expected fields for a tweet.
The actual parsed NSDictionary only include about 5 fields. This happens only in the search method which I’m guessing is the one handled with
YAJL
2010-04-11 19:27:30.596 App[2488:207] url is: http://search.twitter.com/search.json?rpp=100&geocode=37.439974%2C-97.558594%2C2500.000000km
2010-04-11 19:27:30.596 App[2488:207] getSearchResultsForQuery: connectionIdentifier = F891FA7C-3FF7-4B30-A29D-3CCDE97AC117
2010-04-11 19:27:32.987 App[2488:207] Request succeeded for connectionIdentifier = F891FA7C-3FF7-4B30-A29D-3CCDE97AC117
2010-04-11 19:27:33.000 App[2488:207] adding tweet screenname : MrChefTony and tweetText : (null)
In this case for example “from_user” is available but not “text”.
Anyway just thought I’d ask in case is an easy fix or something you were not aware of.
Thanks
Raul
I have replaced YAJL with The JSON parser following this tutorial and now everything works as expected http://gist.github.com/raw/278591/f92d32405d5d259a95f5484aa0c78221caa371c2/gistfile1.m
The data format has changed for the twitter search api and this other library handles any json returned by the twitter api and passes it through to the delegate
hi, thanks for posting the solution.
i have just come across with the same JSON parsing issue and your solution works like a charm and saved me lots of time.
many thanks.
Hi!
I just tried to use your API for iPhone, but when I ran it I got a lot of errors. I understood that there were a librairy to include, but I didn’t find it (no file name libxml2.dylib).
Could you give me a hand on this?
PS : I’m French, excuse me for my bad English…
@Hadrian maybe this can help you
http://www.ashlux.com/wordpress/2009/10/04/compiling-mgtwitterengine-for-the-iphone-using-xcode/
Bye
Hi I was trying below method for twitter
[twitterEngine getSearchResultsForQuery:@"#SHW"];
it returns successfully and gets all the tweet containing #SHW but after parsing it does not give text of tweets.
it gives data like this
{
“created_at” = 1271845298;
“from_user” = sakshiPP;
“profile_image_url” = “http://s.twimg.com/a/1271811071/images/default_profile_3_normal.png”;
“result_type” = recent;
“source_api_request_type” = 35;
},
{
“created_at” = 1271723484;
“from_user” = fashionerd88;
“profile_image_url” = “http://a3.twimg.com/profile_images/829651737/profile_image_1271483952806_normal.jpg”;
“result_type” = recent;
“source_api_request_type” = 35;
},
{
“created_at” = 1271626372;
“from_user” = cancaonova;
“profile_image_url” = “http://a1.twimg.com/profile_images/62516866/trt_normal.jpg”;
“result_type” = recent;
“source_api_request_type” = 35;
},
{
“created_at” = 1271480096;
“from_user” = fashionerd88;
“profile_image_url” = “http://a3.twimg.com/profile_images/829651737/profile_image_1271483952806_normal.jpg”;
“result_type” = recent;
“source_api_request_type” = 35;
},
here text node is missing but when i checked returned data before parsing text was there
{“results”:[{“profile_image_url”:”http://s.twimg.com/a/1271811071/images/default_profile_3_normal.png”,”created_at”:”Wed, 21 Apr 2010 10:21:38 +0000″,”from_user”:”sakshiPP”,”metadata”:{“result_type”:”recent”},”to_user_id”:null,”text”:”#SHW my first tweet”,”id”:12568828849,”from_user_id”:111356857,”geo”:null,”iso_language_code”:”en”,”source”:”<a href="http://twitter.com/">web</a>”},{“profile_image_url”:”http://a3.twimg.com/profile_images/829651737/profile_image_1271483952806_normal.jpg”,”created_at”:”Tue, 20 Apr 2010 00:31:24 +0000″,”from_user”:”fashionerd88″,”metadata”:{“result_type”:”recent”},”to_user_id”:null,”text”:”MUCHO GRACIAS 2ALL THE GUYS THT ACTUALLY PURSUE A LADY N REALLY #SHW HER THT WAH U SAY IS #TRUE #SUBLIMINALTWEET”,”id”:12485375733,”from_user_id”:98837802,”geo”:null,”iso_language_code”:”en”,”source”:”<a href="http://twidroid.com" rel="nofollow">twidroid</a>”},
so it shows that parsing is not dont properly.
but i have not done any change in code.
Can anyone say what could be the reason?
I have been trying for last 1 day please if anyone have any idea ,help me
Hi Matt,
I am also not able to see the tweet text, its coming null. The situation is troublesome because one of my app which is on app-store is also showing tweets as null now. This seems to stop working. Is there a fix for this or I have to get new source.
Thanks,
Madhup
Hi!
Im trying to put an twitter-search option (so no sign-in) in my iphone application, for an assignment, but dont understand how to do so. All the resources seem to be for webbased applications.. Can somebody please help me get started?Would be very much appriciated!!!
Thanks!
Hi :)
Not sure if you’ve fixed this already – the users list obtained from followers/following commands is not having the internal object parsed correctly using LibXML.
Add the following to: _userDictionaryForNodeWithName
else if (xmlStrEqual(name, BAD_CAST “status”))
{
[dictionary setObject:[self _userDictionaryForNodeWithName:name]
forKey:[NSString stringWithUTF8String:(const char *)name]];
}
Trying to get this to work for an iPhone app. I’ve spent a number of hours trying to get this to compile with no luck. It seems that this library assumes you have an OAuth and YAJL libraries installed, is that correct? I have followed the iPhone installation instructions to the T and am getting tons of compile errors (OAuth.h not found, etc). Can anyone post a detailed instruction set for how they got this to work? The provided instructions most definitely do not work.
any luck figuring this out?
duh, get the oauth framework. :)
http://code.google.com/p/oauthconsumer/wiki/UsingOAuthConsumer
Hi,
I started using the MGTwitterEngine in my Twitter client app, but I have some begginers doubts.
I got the login working and got to send a status working also.
But I have two questions::
1. What would be the best way to have a persistent login through the entire app. I mean, I login on the RootView Controller and I don’t want to have to login every time I want to update the status of the people I am following.
2. What would be the best way to populate a tableview with the status of the people I am following?, Does anybody have an example?
Thanx in advance.
[...] MGTwitterEngine and all sorts of other clever components by Matt Legend Gemmell [...]
I like your code!
Do you have a list of iPhone Apps that use your framework and made it successfully to the App Store?
Thanks!
Best regards.
[...] as simple native Cocoa objects (NSArrays, NSDictionarys, NSStrings, NSDates and so on), …Read More [...]
[...] in your iPhone or desktop app: CocoaREST, a generalized superset/replacement for libraries such as MGTwitterEngine: Recently I created a set of Cocoa classes that let developers interact with internet services [...]
I’m seeing the same problem as Ted, loads of errors related to OAuth, starting with OAToken.h
http://skitch.com/jasonpurdy/dqe1k/mgtwitterengine.h-pbr-build-results
Hey Jason, this might help you:
http://github.com/bengottlieb/Twitter-OAuth-iPhone
thanks yehuda! i’ll try this out now…
Hey, this looks very cool, does anybody know if it is also OAuth compatible? Twitter will shut down the old direct request method rather soon (so it says on their dev site).
If it is OAuth compatible, is there a tut that explains how to use it together with an OAuth library or something alike?
Thanks!
Hi Matt,
I am trying to compile project and I have problem with yajl objects. Looks like they missing in project.
Bohdan
[...] code. Although many people are glad that he doesn’t charge for his source so far, especially MGTwitterEngine which powers quite a few iPhone twitter [...]
HI,
i want to show user following and follwers in my iphone app… please help me regarding this
Thanks
Subrat
Hi,
How to show all tweets of user in my iphone .. using twitter Engine.
thanks
Subrat
“Hi! It’s not working!”
- it will be very useful post. As same as your 4-steps-look-how-its-simple help.txt. I’m getting 32 to 118 errors and after few hours see no solution how to fix it.
It will be really helpful to extend your help file. ,)
Have a nice day.
Is there any readme/tutorial for implementing this to Mac app? Where do I get all the missing files and why there has to be such amount of them? Thanks.