Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tutorials for using HTTP POST and GET on the iPhone in Objective-C [closed]

I downloaded apple's demo for using HTTP POST and GET (Their sample app has a tabbar with different parts) and the code is so confusing!

Could anybody give me some sample code or a link to some tutorials about it? :)

Thanks!

like image 989
tarnfeld Avatar asked Feb 27 '10 09:02

tarnfeld


People also ask

How do I send a post request in Objective C?

To make sure you don't have to set a params related to authentication or maybe the application/json. Just adding the parameters I wrote in (NSString post) to the URL is enough when using a browser. try NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];.

How do I use NSURLSession in Objective C?

NSURLSession introduces a new pattern to Foundation delegate methods with its use of completionHandler: parameters. This allows delegate methods to safely be run on the main thread without blocking; a delegate can simply dispatch_async to the background, and call the completionHandler when finished.


2 Answers

This walkthrough by Matt Long is particularly good: http://www.cimgf.com/2010/02/12/accessing-the-cloud-from-cocoa-touch/

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]          initWithURL:[NSURL          URLWithString:@"http://www.cimgf.com/testpost.php"]];   [request setHTTPMethod:@"POST"]; [request setValue:@"text/xml"                forHTTPHeaderField:@"Content-type"];   NSString *xmlString = @"<data><item>Item 1</item><item>Item 2</item></data>";   [request setValue:[NSString stringWithFormat:@"%d",         [xmlString length]]          forHTTPHeaderField:@"Content-length"];   [request setHTTPBody:[xmlString          dataUsingEncoding:NSUTF8StringEncoding]];   [[NSURLConnection alloc]          initWithRequest:request                     delegate:self]; 
like image 130
Dirk Stoop Avatar answered Sep 27 '22 02:09

Dirk Stoop


this is a simple way to use GET:

   NSURL *url = [NSURL URLWithString:@"http://www.32133.com/test?name=xx"];    NSData *data = [NSData dataWithContentsOfURL:url];    NSString *ret = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];    NSLog(@"ret=%@", ret); 
like image 24
navins Avatar answered Sep 24 '22 02:09

navins