謝謝你回答Seva。 我做了一個方法出來的代碼,希望這可以幫助別人:)
//-------------------------------------------------------------------
// 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];
}
使用它
NSMutableArray *webViewParams = [NSMutableArray arrayWithObjects:
@"paramName1", @"paramValue1",
@"paramName2", @"paramValue2",
@"paramName3", @"paramValue3",
nil];
[self UIWebViewWithPost:self.webView url:@"http://www.yourdomain.com" params:webViewParams];
這樣比較好。 – 2010-01-04 23:05:26
這是我嘗試過的(請參閱OP中的鏈接),我嘗試使用connectionDidFinishLoading方法來執行類似[uiWebViewThing loadRequest:responseData]的操作,但它是錯誤的數據類型。響應數據是NSMutable,但它需要NSUrlRequest - 我必須做一些明顯的錯誤。 – Gazzer 2010-01-04 23:17:06
上面的代碼使用NSURLRequest。你需要使用一個NSMutableURLRequest。不要亂用connectionDidFinishLoading,只需使用-loadRequest將其加載到webView中即可。 – 2010-01-04 23:51:12