Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 31st, 2012  |  syntax: Objective C  |  size: 2.88 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. #import "ViewController.h"
  3.  
  4. @interface ViewController : UIViewController
  5.  
  6. @property (strong, nonatomic) IBOutlet UILabel *label;
  7. @property (nonatomic, strong) NSMutableData *receivedData;
  8.  
  9. - (IBAction)doit:(id)sender;
  10.  
  11. @end
  12.  
  13.  
  14. @implementation ViewController
  15. @synthesize label;
  16. @synthesize receivedData;
  17.  
  18. - (void)viewDidLoad
  19. {
  20.     [super viewDidLoad];
  21.         // Do any additional setup after loading the view, typically from a nib.
  22.    
  23. }
  24.  
  25. - (void)viewDidUnload
  26. {
  27.     [self setLabel:nil];
  28.     [super viewDidUnload];
  29.     // Release any retained subviews of the main view.
  30. }
  31.  
  32. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  33. {
  34.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  35. }
  36.  
  37. - (IBAction)doit:(id)sender {
  38.    
  39.     // Create the request.
  40.     NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.inductiveapps.com/posttest.php?hax=yeah"]
  41.                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
  42.                                           timeoutInterval:60.0];
  43.     // create the connection with the request
  44.     // and start loading the data
  45.     NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  46.     if (theConnection) {
  47.         // Create the NSMutableData to hold the received data.
  48.         // receivedData is an instance variable declared elsewhere.
  49.         receivedData = [NSMutableData data];
  50.     } else {
  51.         // Inform the user that the connection failed.
  52.     }
  53. }
  54.  
  55. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  56. {
  57.     // This method is called when the server has determined that it
  58.     // has enough information to create the NSURLResponse.
  59.    
  60.     // It can be called multiple times, for example in the case of a
  61.     // redirect, so each time we reset the data.
  62.    
  63.     // receivedData is an instance variable declared elsewhere.
  64.     [receivedData setLength:0];
  65. }
  66.  
  67. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  68. {
  69.     // Append the new data to receivedData.
  70.     // receivedData is an instance variable declared elsewhere.
  71.     [receivedData appendData:data];
  72. }
  73.  
  74. - (void)connection:(NSURLConnection *)connection
  75.   didFailWithError:(NSError *)error
  76. {
  77.     // inform the user
  78.     NSLog(@"Connection failed! Error - %@ %@",
  79.           [error localizedDescription],
  80.           [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
  81. }
  82.  
  83. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  84. {
  85.     // do something with the data
  86.     [label setText:[[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding]];
  87.     // receivedData is declared as a method instance elsewhere
  88.     NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
  89.  
  90. }
  91.  
  92. @end