Skip to content

iPhone UIWebView POST data

April 19, 2010

A great way to send large amounts of text or just data in general is POST data. It’s easily accessed within a web-based PHP script using the $_POST[] array. For example, if you send a POST value called “mystring”, you could access it through PHP by $_POST[‘mystring’].

To send the POST data to a web page through a WebView within objective-c, you need to first create a NSMutableURLRequest variable that will store this data before it is sent to the external script. Also, you’ll need a formatted (or non-formatted) NSString holding the parameter values themselves. Here’s some code:

NSString *params = [[NSString alloc] initWithFormat:@”mystring=%@”, @”Hello from POST data in Objective-C!”]; —setting up our POST parameter named “mystring” and giving it the whole “Hello…” string value as well.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@”http://myurl.com/myscript.php”%5D%5D; —initializing our URLRequest with the URL to our web-based script.

[request setHTTPMethod:@”POST”];

[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; —send over our POST data string to the external web script.

[webView loadRequest:request]; —loading the request into the UIWebView.

[params release]; —nothing new here, just doing some standard memory management 🙂

[request release];

That’s it! It’s quite simple!

Thanks for visiting my blog!


Advertisements

From → regular

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: