UIWebView initialisation with post variables


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //-------------------------------------------------------------------
  2. // UIWebViewWithPost
  3. // init a UIWebview With some post parameters
  4. //-------------------------------------------------------------------
  5. - (void)UIWebViewWithPost:(UIWebView *)uiWebView url:(NSString *)url params:(NSMutableArray *)params
  6. {
  7. NSMutableString *s = [NSMutableString stringWithCapacity:0];
  8. [s appendString: [NSString stringWithFormat:@"<html><body onload=\"document.forms[0].submit()\">"
  9. "<form method=\"post\" action=\"%@\">", url]];
  10. if([params count] % 2 == 1) { NSLog(@"UIWebViewWithPost error: params don't seem right"); return; }
  11. for (int i=0; i < [params count] / 2; i++) {
  12. [s appendString: [NSString stringWithFormat:@"<input type=\"hidden\" name=\"%@\" value=\"%@\" >\n", [params objectAtIndex:i*2], [params objectAtIndex:(i*2)+1]]];
  13. }
  14. [s appendString: @"</input></form></body></html>"];
  15. //NSLog(@"%@", s);
  16. [uiWebView loadHTMLString:s baseURL:nil];
  17. }
  18.  
  19. useage
  20. NSMutableArray *webViewParams = [NSMutableArray arrayWithObjects:
  21. @"paramName1", @"paramValue1",
  22. @"paramName2", @"paramValue2",
  23. @"paramName3", @"paramValue3",
  24. nil];
  25. [self UIWebViewWithPost:self.webView url:@"http://www.yourdomain.com" params:webViewParams];

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.