如何在UIWebView中显示身份验证质询?

我正在尝试通过UIWebView访问安全的网站。当我通过野生动物园访问它时,我遇到了身份验证挑战,但是在应用程序的UIWebView中没有出现相同的问题。如何使其显示?

任何指针,示例代码或链接都将非常有帮助。非常感谢。


阿波罗的战车
浏览 425回答 3
3回答

沧海一幻觉

实际上,这非常容易...我确定您可以在显示auth挑战委托时(或在加载URL之前,如果您确定所击中的URL会提示您输入auth登录信息,则只需显示UIAlertView)即可。 )。无论如何,诀窍是创建自己的方法NSURLConnection,我做一些逻辑来保存是否使用了auth委托。- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;{    NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authed);    if (!_authed) {        _authed = NO;        /* pretty sure i'm leaking here, leave me alone... i just happen to leak sometimes */        [[NSURLConnection alloc] initWithRequest:request delegate:self];        return NO;    }    return YES;}- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;{    NSLog(@"got auth challange");    if ([challenge previousFailureCount] == 0) {        _authed = YES;        /* SET YOUR credentials, i'm just hard coding them in, tweak as necessary */        [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];    } else {        [[challenge sender] cancelAuthenticationChallenge:challenge];    }}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;{    NSLog(@"received response via nsurlconnection");    /** THIS IS WHERE YOU SET MAKE THE NEW REQUEST TO UIWebView, which will use the new saved auth info **/    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:]];    [_webView loadRequest:urlRequest];}- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;{    return NO;}

慕桂英4014372

如您所知,UIWebView不会提供与服务器通信的机会。我以这种方式解决了这个问题:在UIWebView的委托方法shouldStartLoadWithRequest中,我发起了另一个与NSURLConnection的连接,而在委托NSURLConnection的方法中,didReceiveAuthenticationChallenge已经处理了来自服务器的挑战。在方法didReceiveResponse中(如果挑战来了),然后再次在同一UIWebView中加载相同的URL(挑战已被处理:)。不要忘记在didReceiveResponse中取消连接,否则它将使流量增加一倍。
打开App,查看更多内容
随时随地看视频慕课网APP