Implementación de iOS4 de: ?

¿Cómo puedo implementar -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:] para iOS <5?

 #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0 #import <objc/runtime.h> #import "NSURLConnection+iOS4.h" // Dynamically add -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]. void *sendAsynchronousRequest4(id self, SEL _cmd, NSURLRequest *request, NSOperationQueue *queue, void (^handler)(NSURLResponse*, NSData*, NSError*)); void *sendAsynchronousRequest4(id self, SEL _cmd, NSURLRequest *request, NSOperationQueue *queue, void (^handler)(NSURLResponse*, NSData*, NSError*)) { // How should we implement this? } @implementation NSURLConnection (SendAsync) + (void)load { SEL sendAsyncSelector = @selector(sendAsynchronousRequest:queue:completionHandler:); if (![NSURLConnection instancesRespondToSelector:]) { class_addMethod([self class], sendAsyncSelector, (IMP)sendAsynchronousRequest4, "v@:@@@"); } } @end #endif 

 // NSURLConnection+SendAsync.h #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0 #import <Foundation/Foundation.h> @interface NSURLConnection (SendAsync) @end #endif // NSURLConnection+SendAsync.m #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0 typedef void (^URLConnectionCompletionHandler)(NSURLResponse *response, NSData *data, NSError *error); @interface URLConnectionDelegate : NSObject <NSURLConnectionDataDelegate> @property (nonatomic, strong) NSURLResponse *response; @property (nonatomic, strong) NSMutableData *data; @property (nonatomic, strong) NSOperationQueue *queue; @property (nonatomic, copy) URLConnectionCompletionHandler handler; @end @implementation URLConnectionDelegate @synthesize response; @synthesize data; @synthesize queue; @synthesize handler; - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)theResponse { self.response = theResponse; [data setLength:0]; // reset data } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData { [data appendData:theData]; // append incoming data } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [UIApplication shanetworkingApplication].networkActivityIndicatorVisible = NO; self.data = nil; if (handler) { [queue addOperationWithBlock:^{ handler(response, nil, error); }]; } } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [UIApplication shanetworkingApplication].networkActivityIndicatorVisible = NO; // TODO: Are we passing the arguments to the block correctly? Should we copy them? if (handler) { [queue addOperationWithBlock:^{ handler(response, data, nil); }]; } } @end #import <objc/runtime.h> #import "NSURLConnection+SendAsync.h" // Dynamically add @property (nonatomic,readonly) UIViewController *presentingViewController. void sendAsynchronousRequest4(id self, SEL _cmd, NSURLRequest *request, NSOperationQueue *queue, URLConnectionCompletionHandler handler); void sendAsynchronousRequest4(id self, SEL _cmd, NSURLRequest *request, NSOperationQueue *queue, URLConnectionCompletionHandler handler) { URLConnectionDelegate *connectionDelegate = [[URLConnectionDelegate alloc] init]; connectionDelegate.data = [NSMutableData data]; connectionDelegate.queue = queue; connectionDelegate.handler = handler; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:connectionDelegate]; NSAssert(connection, nil); } @implementation NSURLConnection (SendAsync) + (void)load { SEL sendAsyncSelector = @selector(sendAsynchronousRequest:queue:completionHandler:); if (![NSURLConnection instancesRespondToSelector:sendAsyncSelector]) { class_addMethod(object_getClass([self class]), sendAsyncSelector, (IMP)sendAsynchronousRequest4, "v@:@@@"); } } @end #endif 

Normalmente hago una subclass de NSOperation y hago la request de synchronization dentro del método principal de la operación. Para invocar la operación, creo una instancia de la subclass y la lanzo a una queue (no al hilo principal). Luego, cuando la operación finaliza, llama a un delegado con los datos recibidos o publica una notificación a través de NSNotificationCenter.

Me gustaría ir con un marco ya construido como ASIHTTPRequest (Ya no se está desarrollando, pero aún así es muy bueno) o RestKit (nunca lo he usado antes, pero he escuchado que es decente). Estos le darán la misma funcionalidad (requestes asíncronas http) en diferentes versiones del sistema operativo.

ifdefs no son el path a seguir, ya que están terminados en time de compilation y no comstackrás versiones separadas de la aplicación para cada plataforma.

Probablemente podrías hacer algunas travesuras en time de ejecución para que funcione, pero parece ser más difícil de lo que valía la pena.