IOS > Open URL in Safari with POST

元气小坏坏 提交于 2019-12-12 22:13:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!