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.
Update: Since I made this post, MGTwitterEngine has been updated several times, so be sure to read on for more details. There’s now an iPhone version too, 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.
Give it a shot and see what you think - you can grab the code from my public subversion repository at svn.cocoasourcecode.com (please do use the code in the repository since it’s always up to date, but if you really want to just download a zip file then you can get one here). Maybe you could make the next Twitterrific?
(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: There’s now an iPhone version of MGTwitterEngine, courtesy of Pedro Cuenca (pcuenca on Twitter). You can download the iPhone version here. Here are Pedro’s notes:
The attached archive contains all the modifications plus a Makefile intended for the iPhone.
The main differences are:
- Protocols are not well supported by current iPhone toolchains, and apps that use them tend to crash. I’ve had to include a conditional compilation flag that removes protocol declarations when compiling for the iPhone. This means that delegate instances are simply of type “id”. Instead of calling the delegate methods directly, I’m using respondsToSelector / performSelector pairs to invoke them dynamically. Delegate invocations in MGTwitterXMLParser should be handled using NSInvocation, but I did not write that code yet. Therefore, compilation for the iPhone will yield a couple of warnings.
- The NXSMLParser class has to be loaded dynamically, using NSClassFromString. The same happens for NSDateFormatter.
- Class init methods have been slightly modified to suppress strict compiler warnings.
- New main_iphone.m file, and implementation of applicationDidFinishLaunching: method in AppController.m
Awesome. Many thanks to Pedro for that!
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.
11 Comments