-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRSURLLoader.m
More file actions
114 lines (88 loc) · 2.69 KB
/
RSURLLoader.m
File metadata and controls
114 lines (88 loc) · 2.69 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// RSURLLoader.m
// RSNetflixEngine
//
// Created by Rizwan on 5/29/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "RSURLLoader.h"
@implementation RSURLLoader
- (id)initWithURL:(NSString *)inURL delegate:(id)inDelegate
{
if ((self = [super init])) {
urlRequest = [[NSURLRequest requestWithURL:[NSURL URLWithString:inURL]] retain];
connection = [[NSURLConnection connectionWithRequest:urlRequest delegate:self] retain];
receivedData = [[NSMutableData data] retain];
statusCode = 0;
delegate = inDelegate;
}
return self;
}
- (void)dealloc {
[urlRequest release];
[connection release];
[receivedData release];
[contextObj release];
statusCode = 0;
delegate = nil;
[super dealloc];
}
- (void)start
{
[connection start];
}
- (void)setContext:(NSObject *)context
{
NSObject *tmp = contextObj;
contextObj = [context copy];
[tmp release];
}
- (NSObject *)context
{
return [contextObj copy];
}
#pragma -
#pragma NSURLConnection Delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Connection did finish loading");
/*
NSString *dataString = nil;
if([receivedData length] > 0) {
// Pull the data out
dataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@",dataString);
[dataString release];
}
*/
if(statusCode == 200) {
// Great success! is NAIS!
} else {
// Didn't receive a 200 Response
}
if([delegate respondsToSelector:@selector(loader:didFinishWithStatusCode:data:)]) {
[delegate loader:self didFinishWithStatusCode:statusCode data:receivedData];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSLog(@"Connection did receive response %@", httpResponse);
statusCode = httpResponse.statusCode;
// it can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is declared as a method instance elsewhere
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection did fail with error %@", [error description]);
if([delegate respondsToSelector:@selector(loader:didFailWithError:)]) {
[delegate loader:self didFailWithError:error];
}
}
@end