gpt4 book ai didi

iphone - iOS4 实现-[NSURLConnection sendAsynchronousRequest :queue:completionHandler:]?

转载 作者:搜寻专家 更新时间:2023-10-30 20:21:32 25 4
gpt4 key购买 nike

如何为 iOS <5 实现 -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]

#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 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 <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

关于iphone - iOS4 实现-[NSURLConnection sendAsynchronousRequest :queue:completionHandler:]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9829653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com