Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)fifthAttempt {
- // Create the request.
- NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png"]
- cachePolicy:NSURLRequestUseProtocolCachePolicy
- timeoutInterval:60.0];
- // Create the NSMutableData to hold the received data.
- // receivedData is an instance variable declared elsewhere.
- receivedData = [NSMutableData dataWithCapacity: 0];
- // create the connection with the request
- // and start loading the data
- NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
- if (!theConnection) {
- // Release the receivedData object.
- receivedData = nil;
- // Inform the user that the connection failed.
- }
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- // Append the new data to receivedData.
- // receivedData is an instance variable declared elsewhere.
- [receivedData appendData:data];
- }
- - (void)connection:(NSURLConnection *)connection
- didFailWithError:(NSError *)error
- {
- // Release the connection and the data object
- // by setting the properties (declared elsewhere)
- // to nil. Note that a real-world app usually
- // requires the delegate to manage more than one
- // connection at a time, so these lines would
- // typically be replaced by code to iterate through
- // whatever data structures you are using.
- theConnection = nil;
- receivedData = nil;
- // inform the user
- NSLog(@"Connection failed! Error - %@ %@",
- [error localizedDescription],
- [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- // do something with the data
- // receivedData is declared as a property elsewhere
- NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
- // Release the connection and the data object
- // by setting the properties (declared elsewhere)
- // to nil. Note that a real-world app usually
- // requires the delegate to manage more than one
- // connection at a time, so these lines would
- // typically be replaced by code to iterate through
- // whatever data structures you are using.
- theConnection = nil;
- receivedData = nil;
- }
Add Comment
Please, Sign In to add comment