-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDNURLConnection.m
More file actions
71 lines (59 loc) · 2.1 KB
/
DNURLConnection.m
File metadata and controls
71 lines (59 loc) · 2.1 KB
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
//
// DNURLConnection.m
// MZ Proto
//
// Created by Darren Ehlers on 8/3/13.
// Copyright (c) 2013 DoubleNode.com. All rights reserved.
//
#import "DNURLConnection.h"
@implementation DNURLConnection
+ (id)connectionWithRequest:(NSURLRequest*)request
completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
{
return [[DNURLConnection alloc] initWithRequest:request completionHandler:handler];
}
- (id)initWithRequest:(NSURLRequest*)request
completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
{
self = [super init];
if (self)
{
_handler = handler;
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
return self;
}
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
_response = response;
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to the instance variable you declared
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse
{
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
(_handler)(_response, _responseData, nil);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// The request has failed for some reason!
// Check the error var
(_handler)(_response, _responseData, error);
}
@end