AFNetworking

I started experimenting more with AFNetworking and have been very impressed. I used ASIHTTPRequest almost exclusively in the past but AFNetworking is lighter and easier to wrap my head around. The library has matured fast and I’ve rewritten much of this article to reflect changes.

There is a great example projects that is included with AFNetworking. Checkout AFGowallaAPIClient.m for an example of communicating with a JSON service requiring and API key. This approach could be extended for another service like S3 or Twitter. There is an article posted at HighOnCoding which discusses using AFNetworking to search Twitter.

The NSURLConnectionDelegate methods are implemented inside AFURLConnectionOperation which is extended by several other classes such as AFJSONRequestOperation and AFXMLRequestOperation. All of these classes have convenience methods which take in a success and error block to handle callbacks after executing an NSURLRequest. For example, you might request some data with the following:

NSURLRequest *request = [NSURLRequest requestWithURL:aURL];

AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:request 
  success:^(id object) {
  // handle success
} failure:^(NSHTTPURLResponse *response, NSError *err) {
  // handle error
}];

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];

The AFGowallaAPIClient is an extension of AFHTTPClient which is where the real ma

Something else to keep on eye on is a fork by jakemarsh. Jake’s fork adds AFCallback to AFHTTPOperation to make success and error handling more generic and uses QHTTPOperation from Apple to handle URL connections. Update: Another fork by steipete introduces AFURLCache for improved caching support. AFURLCache is a forked version of SDURLCache.

AFNetworking is awesome. Give it a spin in your next iOS project.