Return to Snippet

Revision: 39790
at January 21, 2011 01:49 by louisdedecker


Initial Code
//-------------------------------------------------------------------
//  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];
}

useage
NSMutableArray *webViewParams = [NSMutableArray arrayWithObjects:
                                 @"paramName1", @"paramValue1",
                                 @"paramName2", @"paramValue2",
                                 @"paramName3", @"paramValue3", 
                                 nil];
[self UIWebViewWithPost:self.webView url:@"http://www.yourdomain.com" params:webViewParams];

Initial URL

                                

Initial Description

                                

Initial Title
UIWebView initialisation with post variables

Initial Tags
iphone

Initial Language
Objective C