An HTTP Fetcher Class for Mac OS X and iOS Apps

Wednesday, September 15, 2010 at 2:56 PM

By Greg Robbins, Software Engineer

(Editor's note: Today's blog post is another cool one for programmers in the audience. Non-developer readers, thanks for bearing with us.)

Mac OS X and iOS provide developers with a high-level class, NSURLConnection, that makes interacting with servers pretty easy. NSURLConnection follows the system networking preferences, navigates through proxies, and handles other details just as users expect on their devices. But in applications that require a series of server requests and responses, NSURLConnection can be inconvenient to use. All connections made by an application’s class call back into a single set of delegate methods, and when there are numerous connections to be made, sorting out the responses is a bit of a chore.

So ever since Google’s first Mac application, Gmail Notifier, we’ve used a wrapper around NSURLConnection to make it easier to issue http requests. After years of being hidden away and evolving inside of other projects, the Google Toolbox for Mac HTTP Fetcher has now been promoted to its own Google Code open source project. Though simple to use for basic GETs and POSTs, it also offers a variety of convenient features for developers of networking applications.

Here is an example of using the fetcher to make a request of a server:

#import "GTMHTTPFetcher.h"

NSURL *url = [NSURL URLWithString:@"http://www.example.com/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request];
[myFetcher beginFetchWithDelegate:self
didFinishSelector:@selector(myFetcher:finishedWithData:error:)];

and when the request is complete, the fetcher invokes a single callback in the application:

- (void)myFetcher:(GTMHTTPFetcher *)fetcher
finishedWithData:(NSData *)retrievedData
error:(NSError *)error {
// if error is not nil, the fetch succeeded
}

One class can initiate many fetches, each with their own callback selectors, making it easy to pack a lot of networking into a single class. In iOS 4 and Mac OS X 10.6, Objective-C blocks simplify fetching even further, avoiding the need for callback methods:

[myFetcher beginFetchWithCompletionHandler:^(NSData *retrievedData,
NSError *error) {
// check the error and use the retrieved data here
}];

As with the underlying NSURLConnection class, the fetcher’s requests are always asynchronous, so the user experience is never blocked waiting for a server response.

This basic http fetcher functionality requires adding just a single source file and header to an application. But the fetcher comes with support for many more advanced features, such as automatic retry on errors, cookie handling, ETag tracking and response caching, convenient request and response logging, and multi-part MIME upload.

To get started using the GTM HTTP Fetcher, read the introduction at the project site, and join the Google Toolbox for Mac discussion group. You can find even more of our open source resources for iOS and Mac developers at the Google Mac Developer Playground.