// // HTTPHelper.h // // Created by Tyler Neylon on 6/20/09. // Copyright 2009 Bynomial. All rights reserved. // // Helper class for working with HTTP connections. // Example synchronous usage: // // NSString* url = @"http://apictureofhouse.com"; // NSString* dataStr; // NSError* error = [[HTTPHelper sharedInstance] synchronousGetURLAsString:url replyData:&dataStr]; // if (error) { // /* handle error */ // } else { // [self doSomethingWith:dataStr]; // } // // Example asynchronous usage (url, dataStr are both type NSString): // // [[HTTPHelper sharedInstance] asyncrhnousGetURLAsString:url delegate:self]; // ... // - (void) httpSuccessWithDataString: (NSString*) dataStr { /* handle success */ } // - (void) httpFailWithError: (NSError*) error { /* handle failure */ } // #import #import "HTTPHelperDelegate.h" typedef enum { HTTPStateNoConnection, HTTPStateAwaitingFullResponse } HTTPState; @interface HTTPHelper : NSObject { NSTimeInterval timeOut; HTTPState state; // private NSMutableData* dataSoFar; NSURLConnection* connection; id delegate; } @property NSTimeInterval timeOut; + (HTTPHelper*) sharedInstance; // Blocks to receive data, or until the timeOut occurs. // If there's another open connection when you call this, that // connection is cancelled to open the new one. - (NSError*) synchronousGetURLAsString: (NSString*) URLAsString replyData: (NSString**)dataStr; // Sends an HTTP request; the reply or error is sent to the delegate_. If delegate_ is nil, // no reply is expected. If there is a pending request already, it is cancelled -- you can // check for a pending request by checking if state == HTTPStateAwaitingFullResponse. // Only one delegate method (either success or failure) will be called per request. - (void) asynchronousGetURLAsString: (NSString*) URLAsString delegate: (id) delegate_; @end