Loading a webpage through UIWebView with POST parameters

jurek epimetheus picture jurek epimetheus · Jul 17, 2009 · Viewed 39.5k times · Source

Is it possible to load a page through UIWebView with POST parameters? I can probably just load an embedded form with the parameters and fill them in with javascript and force a submit, but is there a cleaner and faster way?

Thanks!

Answer

oxigen picture oxigen · Jul 17, 2009

Create POST URLRequest and use it to fill webView

NSURL *url = [NSURL URLWithString: @"http://your_url.com"];
NSString *body = [NSString stringWithFormat: @"arg1=%@&arg2=%@", @"val1",@"val2"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];