Switching from scripting languages to Objective C and iPhone: useful libraries

January 26th, 2009  |  Published in iphone  |  8 Comments

For the last few months I’ve been spending much of my spare hacking time learning to code iPhone applications. I’ve found Objective C to be a surprisingly pleasant language, and Cocoa is one of the best frameworks I’ve ever worked with. I’ve reached a point where I feel I can go fairly quickly from simple app ideas to sketching in real code.

I’m a web developer at heart, and a scripting language user by preference. Coding for the iPhone doesn’t feel as fluid in text handling or HTTP access as the environments I’m used to. Fortunately I’ve been able to find some fantastic open-source libraries and wrappers that make up the difference. Here are my favourites so far:

GTMHTTPFetcher from Google Toolbox for Mac

The iPhone’s native HTTP handling is capable, but low-level and verbose. Rather than handling the many callbacks, NSData objects and options I prefer this wrapper. It has a ton of convenience methods allowing you to specify POST data and basic auth, follow redirects automatically, keep cookies over a session, set headers, and have two simple callbacks for success and error handling. In many ways it’s comparable to jQuery’s $.ajax() one-hit function.

JSON framework

Having got some data over HTTP from a web API, chances are that it’s available in JSON format. This simple framework extends NSString with a JSONValue method to convert any legal JSON string to nested NSDictionaries and NSArrays. To go the other way, dictionaries and arrays gain a JSONRepresentation method.

libxml2 wrappers for XPath over XML and HTML

Perhaps your web API returns XML, or perhaps you’re getting your data by screenscraping HTML. Did you know that the iPhone ships with libxml2, which has high-performance XML and HTML parsing and a high-quality XPath implementation? Don’t struggle with Cocoa’s NSXMLParser or get bogged down in the complex libxml2 docs; use these two simple wrapper functions, PerformXMLXPathQuery and PerformHTMLXPathQuery, to pull out the structured data you need in a Cocoa-friendly representation.

RegexKitLite for regular expressions

Where would scripting be without regular expressions? Luckily they’re available on the iPhone, but buried deep within the ICU libraries. RegexKitLite extends NSString with core regex string handling, including ‘split’ (known as componentsSeparatedByRegex) and a search-and-replace operator (stringByReplacingOccurrencesOfRegex and replaceOccurrencesOfRegex).

FMDB, an Objective C wrapper for sqlite

Every scripting language has convenient database driver wrappers. I was very happy to find that sqlite is available on the iPhone, but unfortunately its interface is all bare-metal C. The simplest wrapper I’ve found so far is FMDB. Apparently somewhat inspired by JDBC, it gives you connection and resultset objects, along with one-liner convenience functions allowing code like [db intForQuery:@"SELECT COUNT(*) FROM things"].

And there’s more…

I’ve used all of the above in a real project, but I’ve got yet more things to explore on my todo list. These include Matt Gemmell’s web-style templating framework MGTemplateEngine, ActorKit for Erlang-style messaging and thread management and the LLVM/Clang Static Analyzer for automatic bug detection. What else do you use?

Responses

  1. Buzz Andersen says:

    January 26th, 2009 at 11:50 pm (#)

    I’d like to nominate whoever made RegExKitLite for a Nobel Prize. It has changed my life as a Cocoa developer.

  2. Infovore » links for January 26th says:

    January 27th, 2009 at 12:00 am (#)

    […] Switching from scripting languages to Objective C and iPhone: useful libraries :: Hackdiary "I’m a web developer at heart, and a scripting language user by preference. Coding for the iPhone doesn’t feel as fluid in text handling or HTTP access as the environments I’m used to. Fortunately I’ve been able to find some fantastic open-source libraries and wrappers that make up the difference. Here are my favourites so far:" A useful – and interesting – set of links from Matt B. (tags: mobile osx iphone development framework objectivec scripting ) […]

  3. On Application Development « random($foo) says:

    January 28th, 2009 at 2:21 am (#)

    […] by the term “dynamic languages”. Yesterday Matt Biddulph posted a bit about some of his experiences as a web developer working with Objective-C and the iPhone (some more discussion), and since […]

  4. Duncan Robertson says:

    January 28th, 2009 at 7:28 pm (#)

    The libxml2 wrappers link is very useful, thanks ..

  5. U Than Soe says:

    January 30th, 2009 at 4:24 pm (#)

    Excellent

  6. a work on process » Selected Saturday links says:

    January 31st, 2009 at 11:59 am (#)

    […] Switching from scripting languages to Objective C and iPhone: useful libraries […]

  7. Ong Jun Da says:

    February 1st, 2009 at 2:32 pm (#)

    GTMHTTPFetcher from Google Toolbox for Mac!

    I wrote my own HTTP wrapper class. Should have googled for a wrapper fist.. Thanks for sharing the libraries!

  8. Morten Bek Ditlevsen says:

    February 3rd, 2009 at 3:14 pm (#)

    I have a question about licensing. I can see that the google toolbox for mac is licensed under apache license 2.0.

    How do you include the license notice as required by apache license 2?

    I have a similar issue with the brilliant “routeme” maps for the iPhone. These are BSD and only require me to include copyright notices, but I cannot figure out to whom I should entitle the copyright notice…

    Thanks for the great pointers!

    Sincerely,
    /morten