*Corrected yajl include path. Updated info about adding YAJL static lib to project for deploying on device
MGTwitterEngine is a great Objective-C library for the Twitter API. Did you know the famous Twitterrific for iPhone (iTunes link) is built with MGTwitterEngine plus YAJL? You probably know that from the now famous YAJL error Twitterrific presented you when the Twitapocalypse hit the world, right?
But setting up MGTwitterEngine and YAJL for iPhone development turned out to be more difficult than I thought, due to the lack of documentation, and my lack of extensive experience with iPhone and Mac development. Anyway, after some hacking around, I finally got the two boys to play together and work with my next cool iPhone project. The following is how I did it and I think it might be useful to someone else wrestling with the same problem out there.
Getting and compiling YAJL on your Mac
First, get the latest YAJL code and get it compiled.
git clone git://github.com/lloyd/yajl
If you don’t have cmake on your Mac yet, get it via MacPorts. (The cmake version from Fink is too old to compile YAJL. I’ve tried it.)
sudo port install cmake
Now compile YAJL
cd yajl sudo ./configure && make install
Now you should have “yajl-1.0.6″ under the “build” folder. And the build process should have copied the binaries to /usr/local/lib/ and /usr/local/include/yajl respectively.
Get the latest code from the SVN repo.
svn checkout http://svn.cocoasourcecode.com/MGTwitterEngine
Adding MGTwitterEngine to your iPhone project
Add everything from the MGTwitterEngine directory starting with ”MGTwitter”, and also the NSString+UUID and NSData+Base64 category files, to the Xcode project. Choose to copy the files to the project.
Telling MGTwitterEngine to use YAJL
MGTwitterEngine doesn’t support Twitter’s search and trends API without YAJL, because these APIs only return JSON data. So obviously we need to tell MGTwitterEngine we have YAJL and please give us the support search and trends APIs, in addition to requesting JSON data for other Twitter API calls.
#define YAJL_AVAILABLE 1
Adding YAJL and LibXML to the project
Now add the required frameworks to the Xcode project.
Locate “/usr/local/lib/libyajl.dylib”, drag and drop it onto “Frameworks” in the Groups & Files pane in Xcode. Don’t choose to copy the files.
Compiling YAJL from the source using the shipped makefile will produce the dynamic library binary files (yajl*.dylib) under /usr/local/lib. Although adding these files to the frameworks gets the project to compile and run on the simulator, this won’t work on a device since only static libraries are allowed on devices for third party frameworks.
Compiling the static library from the source code requires a bit more work but I found someone has already done that with more good stuff like an Obj-C wrapper for YAJL. So we can now simply grab this compiled YAJL static library for iPhone, exand it, then add the file “libYAJLIPhone.a” to the Frameworks.
Then also add “/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/lib/libxml2.dylib” to Frameworks.
Next, open Project Info, locate the Build page, and look for the “Header Search Paths” property. Add the following two paths to the search paths property.
*Although it seems LibXML is not required, I did get compilation errors if LibXML is not in the Frameworks.
Hack and get MGTwitterEngine to build
It appears that MGTwitterEngine is still written against a previous version of YAJL (supposedly a pre-1.0 version). So if at this stage you go ahead and build the project, you will see two errors from MGTwitterEngine.
error: too few arguments to function 'yajl_alloc' error: too few arguments to function 'yajl_free_error'
It looks like the latest version of YAJL has changed its signatures of the functions “yajl_alloc” and “yajl_free_error”. So let’s hack the corresponding code in MGTwitterYAJLParser.m to work around the build errors. Find the calls to these two functions and change the code as follows.
... _handle = yajl_alloc(&callbacks, &cfg, nil, self); ... yajl_free_error(nil, errorMessage); ...
Now you can hit Build and the project should build successfully. Next, you should take a look at AppController.h and AppController.m which come with MGTwitterEngine and see how to use it. It’s pretty easy to use once you get past the compilation problems. Now we have a fully functional and nicely built Twitter library ready to be used for any iPhone project! Also, if you have any suggestion or any better way to get the stuff to work please let me know.