xcode 使用sharedApplication openURL 方法在Safari 中使用POST 方法打开Url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13196192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:59:58  来源:igfitidea点击:

Open Url hosted using POST method in Safari using sharedApplication openURL method

iphoneobjective-ciosxcodeuiapplication

提问by taus-iDeveloper

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 方法在模拟器的 safari 浏览器中托管一个 url。在模拟器中启动我的 iOS 应用程序后,网页在 safari 中打开。为了启动 safari,我使用了 [[UIApplication sharedApplication] openURL: ...

The post method is as follows:

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];

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.

这段代码在使用 UIWebView 时工作正常。但我想使用这个“请求”启动 Safari,它附加了来自 POST 方法的用户名和密码数据。

To launch Safari i must call this code:

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

[[UIApplication sharedApplication] openURL:request];

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

但这显然会引发警告,因为“请求”的类型为 NSMutableURLRequest。“语义问题:不兼容的指针类型将“NSMutableURLRequest *”发送到“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).

我什至不能使用,[[UIApplication sharedApplication] openURL:request.URL];因为这会给我没有附加用户名和密码的 URL(没有 POST 方法)。

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

我想知道如何对这个请求进行类型转换/转换(NSMutableURLRequest),以便我可以在 [[UIApplication sharedApplication] openURL ....

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

希望我对我的问题很清楚。任何帮助将不胜感激。提前致谢 !!

回答by iDev

The recommended approach is to use UIWebViewin your application and create post request using NSURLRequestand 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.

推荐的方法是UIWebView在您的应用程序中使用NSURLRequest并使用和创建发布请求NSURLConnection。创建发布请求并触发请求非常容易。由于您已经弄清楚了这一点并且您不想使用这种方法,因此唯一的替代方法是创建此处提到的临时 html 文件。您可以尝试使用他们遵循的方法,并检查它是否适用于您的情况。

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 NSURLobject as [NSURL fileURLWithPath:path];from this file.

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

Another solution is by creating a one time keyfrom the webserver and pass it to safari via HTTPS request as mentioned here.

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

回答by foggzilla

You cannot pass POST data when launching safari through the openURL method, however you could pass POST data if you use your own UIWebView inside your application instead, like so:

通过 openURL 方法启动 safari 时不能传递 POST 数据,但是如果您在应用程序中使用自己的 UIWebView,则可以传递 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];`

[myUIWebView loadRequest:request];

回答by iCreative

From browser you cant make POST request while adding data into Body. If it is an URL request, where you can add authentication as parameter in header of request, That URL you can open in safari.

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

Hope i am clear.

希望我很清楚。