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.
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.