simple ios url request


/ Published in: iPhone
Save to your folder(s)

Simple synchronous web request for ios.
// Credit to the original authors:
// Sam Walton
// Ethan Irish
// www.seven-labs.com
// Be kind & share :)


Copy this code and paste it in your HTML
  1. // Quick synchronous URL request
  2. NSURL *url = [NSURL URLWithString:@"http://some-internet-place"];
  3. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  4. NSURLResponse *response;
  5. NSError *error;
  6. NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  7. NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  8. // need to check for errors, like if it was a bad call there will be no responsestring
  9. // but your error will have some data
  10. if(!error)
  11. {
  12. NSLog(@"response = %@", responseString);
  13.  
  14. // Take all data and split into array by comma
  15. NSArray* mountArray = [responseString componentsSeparatedByString: @","];
  16. // Just a test var called testItem at the index of 10
  17. NSString* testItem = [mountArray objectAtIndex: 10];
  18. NSLog(@"test item at 10 = %@", testItem);
  19. }
  20. // Authors:
  21. // Sam Walton
  22. // Ethan Irish
  23. // www.seven-labs.com
  24. // Be kind & share :)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.