iOS4 Implementation of -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]?

前端 未结 3 490
孤街浪徒
孤街浪徒 2021-01-03 11:40

How can I implement -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:] for iOS < 5?

#if __IPHONE_OS_VERSION_MIN_REQUIRED <         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-03 11:53

    // NSURLConnection+SendAsync.h
    
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
    
    #import 
    
    @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 
    
    @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 sharedApplication].networkActivityIndicatorVisible = NO;
        self.data = nil;
        if (handler) { [queue addOperationWithBlock:^{ handler(response, nil, error); }]; }
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        [UIApplication sharedApplication].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 
    #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
    

提交回复
热议问题