DanielaM
DanielaM

Reputation: 591

Passing text to a php POST method iphone sdk

I'm developing an iPhone application and I need to send some information to an url from a textview and 2 texfields, using the HTTP POST method. (to, subject, message). How do I do it? I understand it is something with NSURLRequest, but I didn't find anything that could actually help me. So if you could please include a clarifying code example, I would really appreciate it.

Upvotes: 0

Views: 1631

Answers (2)

Dave DeLong
Dave DeLong

Reputation: 243146

I recently wrote a subclass of NSMutableURLRequest to make form POSTing a lot easier. It's on github: http://github.com/davedelong/CHFormRequest

You use it like this:

#import "CHFormRequest.h"

CHFormRequest * r = [[CHFormRequest alloc] initWithURL:[NSURL URLWithString:@"http://example.com/myForm.php"]];

[r setValue:@"test" forFormField:@"field1"];    
[r setFile:@"/Users/example/Desktop/myFile.txt" forFormField:@"file"];

NSHTTPURLResponse * response = nil;
NSError * error = nil;
NSData * d = [NSURLConnection sendSynchronousRequest:r returningResponse:&response error:&error];
[r release];

Upvotes: 1

Related Questions