How do I insert a POST request into a UIWebView
You can use something like ASIHTTPRequest to make the POST request (With the option of doing it asynchronously) and then load the response string/data into the UIWebView. Look at this page under the section titled Sending data with POST or PUT requests and then look at the Creating an asynchronous request section at the top for information on how to handle the response string/data.
Hope that helps, sorry if I misunderstood your question.
You can use an NSMutableURLRequest, set the HTTP method to POST, and then load it into your UIWebView using -loadRequest.
(edited original answer to include newly tested code)
I just wanted to drop in my version of this request. I have used a dictionary to represent the post parameters.
It's a chunk of code but is simple enough to drop into a view with a webview and use for all URL loading. It will only POST if you send a "postDictionary". Otherwise it will use the url you sent it to just GET things.
- (void) loadWebView:(UIWebView *)theWebView withURLString:(NSString *)urlString andPostDictionaryOrNil:(NSDictionary *)postDictionary {
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60.0];
// DATA TO POST
if(postDictionary) {
NSString *postString = [self getFormDataString:postDictionary];
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
}
[theWebView loadRequest:request];
}
- (NSString *)getFormDataString:(NSDictionary*)dictionary {
if( ! dictionary) {
return nil;
}
NSArray* keys = [dictionary allKeys];
NSMutableString* resultString = [[NSMutableString alloc] init];
for (int i = 0; i < [keys count]; i++) {
NSString *key = [NSString stringWithFormat:@"%@", [keys objectAtIndex: i]];
NSString *value = [NSString stringWithFormat:@"%@", [dictionary valueForKey: [keys objectAtIndex: i]]];
NSString *encodedKey = [self escapeString:key];
NSString *encodedValue = [self escapeString:value];
NSString *kvPair = [NSString stringWithFormat:@"%@=%@", encodedKey, encodedValue];
if(i > 0) {
[resultString appendString:@"&"];
}
[resultString appendString:kvPair];
}
return resultString;
}
- (NSString *)escapeString:(NSString *)string {
if(string == nil || [string isEqualToString:@""]) {
return @"";
}
NSString *outString = [NSString stringWithString:string];
outString = [outString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// BUG IN stringByAddingPercentEscapesUsingEncoding
// WE NEED TO DO several OURSELVES
outString = [self replace:outString lookFor:@"&" replaceWith:@"%26"];
outString = [self replace:outString lookFor:@"?" replaceWith:@"%3F"];
outString = [self replace:outString lookFor:@"=" replaceWith:@"%3D"];
outString = [self replace:outString lookFor:@"+" replaceWith:@"%2B"];
outString = [self replace:outString lookFor:@";" replaceWith:@"%3B"];
return outString;
}
- (NSString *)replace:(NSString *)originalString lookFor:(NSString *)find replaceWith:(NSString *)replaceWith {
if ( ! originalString || ! find) {
return originalString;
}
if( ! replaceWith) {
replaceWith = @"";
}
NSMutableString *mstring = [NSMutableString stringWithString:originalString];
NSRange wholeShebang = NSMakeRange(0, [originalString length]);
[mstring replaceOccurrencesOfString: find
withString: replaceWith
options: 0
range: wholeShebang];
return [NSString stringWithString: mstring];
}
Thanks for you answer Seva. I've made a method out of your code, hope this helps other people :)
//-------------------------------------------------------------------
// UIWebViewWithPost
// init a UIWebview With some post parameters
//-------------------------------------------------------------------
- (void)UIWebViewWithPost:(UIWebView *)uiWebView url:(NSString *)url params:(NSMutableArray *)params
{
NSMutableString *s = [NSMutableString stringWithCapacity:0];
[s appendString: [NSString stringWithFormat:@"<html><body onload=\"document.forms[0].submit()\">"
"<form method=\"post\" action=\"%@\">", url]];
if([params count] % 2 == 1) { NSLog(@"UIWebViewWithPost error: params don't seem right"); return; }
for (int i=0; i < [params count] / 2; i++) {
[s appendString: [NSString stringWithFormat:@"<input type=\"hidden\" name=\"%@\" value=\"%@\" >\n", [params objectAtIndex:i*2], [params objectAtIndex:(i*2)+1]]];
}
[s appendString: @"</input></form></body></html>"];
//NSLog(@"%@", s);
[uiWebView loadHTMLString:s baseURL:nil];
}
to use it
NSMutableArray *webViewParams = [NSMutableArray arrayWithObjects:
@"paramName1", @"paramValue1",
@"paramName2", @"paramValue2",
@"paramName3", @"paramValue3",
nil];
[self UIWebViewWithPost:self.webView url:@"http://www.yourdomain.com" params:webViewParams];