source: gs2-extensions/iOS-1.x/trunk/NSURLConnection-example.mm@ 22548

Last change on this file since 22548 was 22548, checked in by davidb, 12 years ago

Original version of Greenstone2 app for iPhone/iPod-touch

File size: 2.2 KB
Line 
1// create the request
2
3NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
4
5 cachePolicy:NSURLRequestUseProtocolCachePolicy
6
7 timeoutInterval:60.0];
8
9// create the connection with the request
10
11// and start loading the data
12
13NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
14
15if (theConnection) {
16
17 // Create the NSMutableData that will hold
18
19 // the received data
20
21 // receivedData is declared as a method instance elsewhere
22
23 receivedData=[[NSMutableData data] retain];
24
25} else {
26
27 // inform the user that the download could not be made
28
29}
30
31- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
32
33{
34
35 // this method is called when the server has determined that it
36
37 // has enough information to create the NSURLResponse
38
39
40
41 // it can be called multiple times, for example in the case of a
42
43 // redirect, so each time we reset the data.
44
45 // receivedData is declared as a method instance elsewhere
46
47 [receivedData setLength:0];
48
49}
50
51- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
52
53{
54
55 // append the new data to the receivedData
56
57 // receivedData is declared as a method instance elsewhere
58
59 [receivedData appendData:data];
60
61}
62
63- (void)connection:(NSURLConnection *)connection
64
65 didFailWithError:(NSError *)error
66
67{
68
69 // release the connection, and the data object
70
71 [connection release];
72
73 // receivedData is declared as a method instance elsewhere
74
75 [receivedData release];
76
77
78
79 // inform the user
80
81 NSLog(@"Connection failed! Error - %@ %@",
82
83 [error localizedDescription],
84
85 [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
86
87}
88
89- (void)connectionDidFinishLoading:(NSURLConnection *)connection
90
91{
92
93 // do something with the data
94
95 // receivedData is declared as a method instance elsewhere
96
97 NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
98
99
100
101 // release the connection, and the data object
102
103 [connection release];
104
105 [receivedData release];
106
107}
Note: See TracBrowser for help on using the repository browser.