问题
I'd like to launch a URL in Safari from UIView sending POST datas. This would allow me to load my Paypal page.
Normally, in HTML we have to do this :
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" style="text-align:center;" target="_blank">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="XXXXXXXXXXX">
<input type="submit" value="Faire un don" id="donpaypal">
</form>
I know that I can open a new URL with this few lines :
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http:://I.have.a.beautifull.website.com"]];
Is there a mean to specify POST datas OR would you have a mean?
回答1:
I suggest you to implement your custom browser using a UIWebView. A UIWebView can load a NSURLRequest. Have a look at the following code
NSString *post = @"yourPostInformation";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:baseURL]];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
[myWebView loadRequest:request];
来源:https://stackoverflow.com/questions/9500000/ios-open-url-in-safari-with-post