使用sharedApplication openURL方法在Safari中使用POST方法托管的Open Url [英] Open Url hosted using POST method in Safari using sharedApplication openURL method
问题描述
我有一种情况,我使用POST方法在模拟器中的safari浏览器中托管一个url。
在模拟器中启动我的iOS应用程序后,在safari中打开网页。为了启动safari,我使用了[[UIApplication sharedApplication] openURL:...
I have a situation in which i am using POST method to host a url in safari browser in simulator. The web page is opened in safari after launching my iOS app in simulator. To launch safari i have used [[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:请求委托:self];
[_urlConnection start];
NSURLConnection* _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [_urlConnection start];
这段代码在使用UIWebView时工作正常。但我想使用此请求启动Safari,该请求附加了用户名&来自POST方法的密码数据。
this piece of code works fine when using UIWebView. But i want to launch Safari using this "request" which has appended username & password data from the POST method.
要启动Safari,我必须调用此代码:
To launch Safari i must call this code:
[[UIApplication sharedApplication] openURL:request];
但是这会引发警告,因为request的类型为NSMutableURLRequest。
语义问题:不兼容的指针类型向'NSURL *'类型的参数发送'NSMutableURLRequest *'..
But this throws a warning obviously because "request" is of type NSMutableURLRequest. "Semantic Issue: Incompatible pointer types sending 'NSMutableURLRequest *' to parameter of type 'NSURL *" ..
我甚至无法使用 [[UIApplication sharedApplication] openURL:request.URL];
因为这会给我一个未附加用户名和&的URL。密码(没有POST方法)。
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).
我想知道如何对此请求进行类型转换/转换(NSMutableURLRequest),以便我可以在[[UIApplication sharedApplication]中容纳它openURL ....
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 !!
推荐答案
推荐的方法是使用 UIWebView
在您的应用程序中,使用 NSURLRequest
和 NSURLConnection
创建发布请求。创建一个post请求并触发请求非常容易。既然你已经想出了这个并且你不想使用这种方法,那么只有通过创建一个临时的html文件 这里提到 。您可以尝试使用他们遵循的方法,并检查它是否适用于您的情况。
The recommended approach is to use UIWebView
in your application and create post request using NSURLRequest
and NSURLConnection
. It is pretty easy to create a post request and fire the request. Since you have already figured this out and you dont want to use this approach, only alternative is by creating a temporary html file as mentioned here. You can try with the approach they are following and check if it is working in your case.
根据该链接,您必须编写一个临时的html文件。这个html文件将有一个 onLoad()
javascript,它会立即发送POST,并为没有启用javascript的用户提供一个按钮。您可以从此文件中 [NSURL fileURLWithPath:path];
创建 NSURL
对象。
As per that link, you have to write a temporary html file. This html file will have an onLoad()
javascript which immediately sends the POST, and a button for users that don't have javascript enabled. You can create NSURL
object as [NSURL fileURLWithPath:path];
from this file.
另一种解决方案是通过创建一次性密钥网络服务器并通过HTTPS请求将其传递给safari 如此处所述。
Another solution is by creating a one time key from the webserver and pass it to safari via HTTPS request as mentioned here.
这篇关于使用sharedApplication openURL方法在Safari中使用POST方法托管的Open Url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!