简体   繁体   English

如何打开 url 需要用户名和密码 uiwebview?

[英]how to open url need username and password with uiwebview?

I want to open an URL which need password and username in a UIWebview.我想在 UIWebview 中打开需要密码和用户名的 URL。 Such as open my local Wifi Router(192.168.1.1).比如打开我本地的Wifi路由器(192.168.1.1)。 But when I try following code, there is no popup as Safari to require password and username.但是当我尝试以下代码时,没有弹出 Safari 要求密码和用户名。

NSURL *url = [NSURL URLWithString:@"http://192.168.1.1"];
NSURLRequest *httpReq = [NSURLRequest requestWithURL:url];
[self._webView loadRequest:httpReq];

Since someone told me to use NSURLConnectionDelegate, I know this, but I donot know how to show the authorized page to the UIWebView.由于有人告诉我使用 NSURLConnectionDelegate,我知道这一点,但我不知道如何将授权页面显示给 UIWebView。

This will help这将有助于

NSURL *url = [NSURL URLWithString:@"http://username:password@192.168.1.1"]; NSURLRequest *httpReq = [NSURLRequest requestWithURL:url]; [self._webView loadRequest:httpReq];

You need to implement connection:didReceiveAuthenticationChallenge: in NSURLConnectionDelegate.你需要在 NSURLConnectionDelegate 中实现 connection:didReceiveAuthenticationChallenge:。 For details, read Authentication Challenges chapter from Apple "URL Loading System Programming Guide".有关详细信息,请阅读 Apple“URL 加载系统编程指南”中的身份验证挑战章节。

UIWebView doesn't provide any mechanism to identify the response. UIWebView 不提供任何机制来识别响应。 So one solution is explained in UIWebViewHttpStatusCodeHandling github project which identifies the status code of the response (should be 404 in your case).因此,在UIWebViewHttpStatusCodeHandling github 项目中解释了一种解决方案,该项目标识了响应的状态代码(在您的情况下应该是 404)。

However, the main drawback is for each request you need to use NSURLConnection and load the request again.但是,主要缺点是对于每个请求,您需要使用 NSURLConnection 并再次加载请求。 But in this case, you can cancel the NSURLConnection too.但在这种情况下,您也可以取消 NSURLConnection。

The other solution should (might) be the use of Java-Script.另一个解决方案应该(可能)是使用 Java 脚本。 Search for the Java-script, which provides the status code of the response.搜索提供响应状态代码的 Java 脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM