iOS Development May 13, 2012

Consuming REST Services from iOS 5

Recently, I've collaborated on developing several applications for iOS (versions 4 and 5). These applications consume REST services that return JSON or XML. While this is a relatively straightforward task, it requires some investigation to do it well because there are many frameworks, formats, and platforms that can be involved.

Choosing ASIHTTPRequest

After investigating for a while, reviewing examples, and reading a lot of code, I ended up selecting the ASIHTTPRequest library. This library is robust and allows me to do things like invoke services asynchronously (which should be mandatory in mobile or desktop UI applications) or consume services that require authentication through headers.

Why ASIHTTPRequest?

  • Robust and battle-tested
  • Supports asynchronous service invocation
  • Handles authentication headers
  • Works with JSON and XML

Setup Instructions

Download the classes, add them to your project, and don't forget to include the reachability class from the Externals folder of ASIHTTPRequest. You'll be ready to consume REST services from iOS... or almost ready.

The iOS 5 ARC Challenge

Almost a year ago, iOS 5 was released, and this will cause you some problems. One of its major innovations was Automatic Reference Counting (ARC) - you no longer have to manually destroy references to unused objects.

Before ARC, we would write instructions like:

[shareDialog release]

Other instructions like retain and dealloc are also no longer necessary when working in ARC mode.

Compatibility Issue

These compiler changes make libraries like ASIHTTPRequest incompatible with ARC. However, the compiler has a flag that can be applied per class file to ignore the ARC feature when compiling that file.

Instructions and screenshots available here

Understanding ARC

It's important to clarify that ARC is a compiler feature, not a runtime feature. It's not similar to .NET's Garbage Collector, though it might be confused with it.

Basically, what's happening is that the compiler adds the memory management instructions to your program at compile time. This means zero runtime overhead while still freeing you from manual memory management.

Final Setup

At this point, your solution should compile successfully, and you have everything necessary to start consuming REST services from your iOS 4 or 5 application.

Ready to Go

With ASIHTTPRequest integrated and ARC compatibility configured, you can now build iOS applications that communicate seamlessly with REST APIs, handling both JSON and XML responses with proper asynchronous patterns.

Related reading

More in the Digest — a weekly roundup of AI and agents news — or start with AI Strategy.