redribben

download image

Oct 26th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. - (void)fifthAttempt {
  2. // Create the request.
  3. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png"]
  4. cachePolicy:NSURLRequestUseProtocolCachePolicy
  5. timeoutInterval:60.0];
  6.  
  7. // Create the NSMutableData to hold the received data.
  8. // receivedData is an instance variable declared elsewhere.
  9. receivedData = [NSMutableData dataWithCapacity: 0];
  10.  
  11. // create the connection with the request
  12. // and start loading the data
  13. NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  14. if (!theConnection) {
  15. // Release the receivedData object.
  16. receivedData = nil;
  17.  
  18. // Inform the user that the connection failed.
  19. }
  20. }
  21.  
  22. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  23. {
  24. // Append the new data to receivedData.
  25. // receivedData is an instance variable declared elsewhere.
  26. [receivedData appendData:data];
  27. }
  28.  
  29.  
  30. - (void)connection:(NSURLConnection *)connection
  31. didFailWithError:(NSError *)error
  32. {
  33. // Release the connection and the data object
  34. // by setting the properties (declared elsewhere)
  35. // to nil. Note that a real-world app usually
  36. // requires the delegate to manage more than one
  37. // connection at a time, so these lines would
  38. // typically be replaced by code to iterate through
  39. // whatever data structures you are using.
  40. theConnection = nil;
  41. receivedData = nil;
  42.  
  43. // inform the user
  44. NSLog(@"Connection failed! Error - %@ %@",
  45. [error localizedDescription],
  46. [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
  47. }
  48.  
  49. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  50. {
  51. // do something with the data
  52. // receivedData is declared as a property elsewhere
  53. NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
  54.  
  55. // Release the connection and the data object
  56. // by setting the properties (declared elsewhere)
  57. // to nil. Note that a real-world app usually
  58. // requires the delegate to manage more than one
  59. // connection at a time, so these lines would
  60. // typically be replaced by code to iterate through
  61. // whatever data structures you are using.
  62. theConnection = nil;
  63. receivedData = nil;
  64. }
Add Comment
Please, Sign In to add comment