Class that uses blocks (new to iOS4/OSX 10.6) to make URL requests/downloads much more easier
Objective-C C
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
OHURLLoader Example
.gitattributes
.gitignore
LICENSE
OHURLLoader.h
OHURLLoader.m
README

README

This class make it easier to perform URL requests by using blocks (instead of having to use a delegate and implement delegate methds).

----------

To use this class, you simply have to use the commodity constructor then call one of the two "start…" methods, providing the blocks of code to execute on success and on error.

	NSURL* url = ...
	NSURLRequest* req = [NSURLRequest requestWithURL:url];

	OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
	[loader startRequestWithCompletion:^(NSData* receivedData, NSInteger httpStatusCode) {
		NSLog(@"Download of %@ done (statusCode:%d)",url,statusCode);
		if (httpStatusCode == 200) {
			// Use comodity accessos receivedString that interpret the received NSData
			// using the encoding mentionned in the received response headers, if available
			outputTextView.text = loader.receivedString;
		} else {
			// Log unexpected status code
			outputTextView.text = [NSString stringWithFormat:@"HTTP Status code: %d",httpStatusCode];
		}
	} errorHandler:^(NSError *error) {
		NSLog(@"Error while downloading %@: %@",url,error);
		outputTextView.text = [error localizedDescription];
	}];

----------

If you want to, you can also provide blocks of code to execute when receiving each chunk of data, to be notified of the progress of the download. This can be useful for long downloads, for example to update a progressbar on the screen:

You may also provide a block of code to execute when the response header is received, at which stage you are able to known the expectedContentLength or the MimeType of the response (and some other headers).

	NSURL* url = ...
	NSURLRequest* req = [NSURLRequest requestWithURL:url];

	OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
	[loader startRequestWithResponseHandler:^(NSURLResponse* response) {
		NSLog(@"Expected ContentLength: %ld",[response expectedContentLength]);
		progressView.progress = 0.f;
	} progress:^(NSUInteger receivedBytes, long long expectedBytes) {
		if (expectedBytes > 0) progressView.progress = receivedBytes / (float)expectedBytes;
	} completion:^(NSData* receivedData, NSInteger httpStatusCode) {
		progressView.progress = 1.f;
		NSLog(@"Download Done (%@, statusCode:%d)",url,httpStatusCode);
		outputTextView.text = loader.receivedString;
	} errorHandler:^(NSError* error) {
		outputTextView.text = [error localizedDescription];
	}];

----------

For a more detailed usage example, see the example project in the "OHURLLoader Example" folder



NOTE FOR PRE-OSX 10.6 and PRE-iOS 4.0
	This class is primarly designed to be used with blocks, which are a new feature introduced in OSX 10.6 and iOS 4.0. 
	Anyway, if you use earlier version of OSX/iOS, you can still use this class and listen for the NSNotifications it emits instead of using blocks.
	In such case, be sure not to use NSNotifications and blocks at the same time (to avoid executing BOTH blocks and notification listener's code).
	You can do this by setting the blocks to nil when using NSNotifications (or by surrounding your "addObserver:..." code by "#if NS_BLOCKS_AVAILABLE" ... "#endif" directives to avoid adding as an observer when using blocks)

NOTE FOR AUTHENTICATION CHALLENGES & CREDENTIALS
	Even if this would be a feature easy to add, the current version of this class does not handle "Authentication Challenges" (when URL requests credentials to be accessed).
	This is a design choice to keep the class API as simple as possible (as this feature is rarely used in practice)