【问题标题】:How to display the Authentication Challenge in UIWebView for local network url?如何在 UIWebView 中显示本地网络 url 的身份验证质询?
【发布时间】:2015-03-30 01:56:19
【问题描述】:
我正在尝试通过 UIWebView 访问安全的本地网络 URL。当我通过 safari 访问它时,我收到了身份验证质询,但同样的问题没有出现在我的应用程序中的 UIWebView 中。我怎样才能让它出现?
例如http://292.168.1.54/TestWeb/Test.pdf
此网址在 safari 浏览器中有效,但相同的网址未出现在我的 UIWebView 中。
任何指针、示例代码或链接都会非常有帮助。非常感谢。
【问题讨论】:
标签:
ios
objective-c
uiwebview
【解决方案1】:
有两种方法可以进行身份验证(在您的情况下可能是基本身份验证)挑战。
-[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:] 会给你这个请求。现在您只需对同一 URL 发起第二个请求并使用 [NSURLConnectionDelegate connection:willSendRequestForAuthenticationChallenge:] 来获得挑战。然后显示一个对话框并要求用户提供凭据。将凭据保存在 NSURLCredentialStorage 中,然后重新加载页面。
Create a subclass of NSURLProtocol 处理 http 和 https。类似于this answer 并在那里获得身份验证挑战。
【解决方案2】:
希望这段代码对你有所帮助。
-
(void)viewDidLoad
{
[超级视图DidLoad];
// 在从 nib 加载视图后做任何额外的设置。
self.webView.delegate = self;
NSURL *url = [NSURL URLWithString:@"http://skinC.com/abc/"];// here you can write your url that you want to open
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestURL];
AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self setNeedsStatusBarAppearanceUpdate];}