-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConnectionDelegate.m
89 lines (71 loc) · 1.97 KB
/
ConnectionDelegate.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
80
81
82
83
84
85
86
87
88
89
//
// ConnectionDelegate.m
// iphone-harvest
//
// Created by vickeryj on 1/14/09.
// Copyright 2009 Joshua Vickery. All rights reserved.
//
#import "ConnectionDelegate.h"
@implementation ConnectionDelegate
@synthesize response, data, error, connection;
- (id) init
{
self = [super init];
if (self != nil) {
self.data = [NSMutableData data];
done = NO;
}
return self;
}
- (BOOL) isDone {
return done;
}
- (void) cancel {
[connection cancel];
self.response = nil;
self.data = nil;
self.error = nil;
done = YES;
}
#pragma mark NSURLConnectionDelegate methods
- (NSURLRequest *)connection:(NSURLConnection *)aConnection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
return request;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] > 0) {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
else {
[[challenge sender] useCredential:[challenge proposedCredential] forAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
done = YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse {
self.response = aResponse;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)someData {
[data appendData:someData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
done = YES;
}
- (void)connection:(NSURLConnection *)aConnection didFailWithError:(NSError *)aError {
self.error = aError;
done = YES;
}
//don't cache resources for now
- (NSCachedURLResponse *)connection:(NSURLConnection *)aConnection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}
#pragma mark cleanup
- (void) dealloc
{
[data release];
[response release];
[error release];
[connection release];
[super dealloc];
}
@end