-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCURLConnection.m
79 lines (57 loc) · 2.58 KB
/
BCURLConnection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#import <objc/runtime.h>
#import "BCURLConnection.h"
typedef void (^BCCallbackBlock)(NSURLResponse*, NSData*, NSError*);
@interface BCURLConnectionHandler : NSObject <NSURLConnectionDelegate>
@property (strong, nonatomic) NSURLConnection *connection;
@property (strong, nonatomic) BCCallbackBlock handler;
@property (strong, nonatomic) NSOperationQueue *queue;
@property (strong, nonatomic) NSMutableData *data;
@property (strong, nonatomic) NSURLResponse *response;
@end
@implementation BCURLConnectionHandler
@synthesize connection, handler = _handler, queue = _queue, data = _data, response = _response;
- (void)performRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler {
self.handler = handler;
if (!queue) queue = [NSOperationQueue mainQueue];
self.queue = queue;
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
BCCallbackBlock handler = self.handler;
self.handler = nil;
NSURLResponse *response = self.response;
[self.queue addOperationWithBlock:^{
handler(response, nil, error);
}];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if (response.expectedContentLength > 0) {
self.data = [NSMutableData dataWithCapacity:response.expectedContentLength];
} else {
self.data = [NSMutableData data];
}
self.response = response;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
BCCallbackBlock handler = self.handler;
self.handler = nil;
NSURLResponse *response = self.response;
NSData *data = [self.data copy];
[self.queue addOperationWithBlock:^{
handler(response, data, nil);
}];
}
@end
@implementation BCURLConnection
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler {
if (class_getClassMethod([self superclass], @selector(sendAsynchronousRequest:queue:completionHandler:)) != NULL) {
[super sendAsynchronousRequest:request queue:queue completionHandler:handler];
} else {
BCURLConnectionHandler *connectionHandler = [[BCURLConnectionHandler alloc] init];
[connectionHandler performRequest:request queue:queue completionHandler:handler];
}
}
@end