使用sharedApplication openURL方法在Safari中使用POST方法托管的Open Url

tau*_*per 8 iphone xcode objective-c uiapplication ios

我有一种情况,我使用POST方法在模拟器中的safari浏览器中托管一个URL.在模拟器中启动我的iOS应用程序后,在safari中打开该网页.为了启动safari,我使用了[[UIApplication sharedApplication] openURL:...

post方法如下:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://***some client url***.jsp"]];

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", userName, password];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];`


NSURLConnection* _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[_urlConnection start];
Run Code Online (Sandbox Code Playgroud)

NSURLConnection*_urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [_urlConnection start];

使用UIWebView时,这段代码可以正常工作.但我想使用这个"请求"启动Safari,该请求附加了POST方法的用户名和密码数据.

要启动Safari,我必须调用以下代码:

[[UIApplication sharedApplication] openURL:request];
Run Code Online (Sandbox Code Playgroud)

But this throws a warning obviously because "request" is of type NSMutableURLRequest. "Semantic Issue: Incompatible pointer types sending 'NSMutableURLRequest*' to parameter of type 'NSURL*" ..

I cannot even use [[UIApplication sharedApplication] openURL:request.URL]; since this will give me the URL which is not appended with username & password(with out POST method).

I want to know how to typecast/convert this request(NSMutableURLRequest) so that i can accommodate it in the [[UIApplication sharedApplication] openURL ....

Hope i am quite clear with my question. Any help will be highly appreciated. Thanks in advance !!

iDe*_*Dev 5

建议的方法是UIWebView在您的应用程序中使用NSURLRequest并使用和创建发布请求NSURLConnection.创建一个post请求并触发请求非常容易.既然你已经想出了这个并且你不想使用这种方法,那么只有替代方法是创建一个临时的html文件,如这里提到的.您可以尝试使用他们遵循的方法,并检查它是否适用于您的情况.

根据该链接,您必须编写一个临时的html文件.这个html文件将有一个onLoad()立即发送POST 的javascript,以及一个没有启用javascript的用户的按钮.您可以从此文件创建NSURL对象[NSURL fileURLWithPath:path];.

另一种解决方案是从网络服务器创建一次性密钥,并通过此处提到的 HTTPS请求将其传递给safari .


iCr*_*ive 2

在将数据添加到 Body 时,您无法从浏览器发出 POST 请求。如果是 URL 请求,您可以在请求标头中添加身份验证作为参数,您可以在 safari 中打开该 URL。

希望我很清楚。