如何在UIWebView中显示身份validation挑战?

我试图通过UIWebView访问一个安全的网站。 当我通过safari访问它时,我得到了一个身份validation的挑战,但是在我的UIWebView中并没有出现在应用程序中。 我怎样才能让它出现?

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

这实际上是超级简单的…我敢肯定你可以显示一个UIAlertView时,显示授权挑战委托(或之前加载的url,如果你确信你打的URL将提示inputvalidationlogin信息)。 无论如何,诀窍是创build自己的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; } 

如你所知,UIWebView不提供与服务器通信的机会。 我通过这种方式解决了这个问题:在UIWebView的委托方法shouldStartLoadWithRequest中,我使用NSURLConnection启动另一个连接,并且在委托NSURLConnection didReceiveAuthenticationChallenge的方法中已经从服务器处理了挑战。 在方法didReceiveResponse(如果挑战来了),然后再次在相同的UIWebView加载相同的URL(挑战已被处理:)。 不要忘记在didReceiveResponse中取消连接,否则会使stream量增加一倍。

您也可以在url中提供您的凭据。 只需在“http://”和“网页url”之间添加用户名和密码。

 NSString *urlString = @"http://username:password@domain.com/home"; [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]]; 

如果你正在经历Sahil的回答中Zach所描述的症状:

就像y5h所说的那样,在didReceiveResponse方法中join`_authed = YES'将停止无限循环。 即使auth没有成功,你也需要把它看作是auth,所以如果不需要validation,它会尝试继续加载页面,如果真的需要validation,那么它就会像正常一样失败。

对于其中shouldStartLoadWithRequest:多次触发(由于网页上的embedded内容)的第二个症状,它将只显示加载的最后一个东西,而不是整个网页,请执行以下操作:

在shouldStartLoadWithRequest:方法中,将其添加到顶部

 if(webview.loading){ //if url requests come through while its loading, its probably embedded content return YES; } 

编辑:这个上面的方法有问题,如果页面完全加载,然后再加载更多的embedded式内容,打破了facebook,这是目前唯一的案例

这将让网站正在尝试加载时通过url。 即时通讯不知道,如果它的安全,假设每个url后,最初的请求是embedded式的内容,但为我的目的,它似乎工作,所以也许它也适合你。

另外,使用

 - (void) connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 

因为

 connection:canAuthenticateAgainstProtectionSpace: connection:didReciveAuthenticationChallenge: connection:didCancelAuthenticationChallenge: 

被删除,对我而言,你无法使用https网站进行身份validation

我想build议使用下面的窗口的另一个答案: https : //github.com/jivesoftware/JiveAuthenticatingHTTPProtocol

它包含一个示例,显示如何使用UIAlertView来询问用户的密码,并可以很容易地从本地数据库中返回保存的密码。

免责声明:我与Jive没有任何关系,也没有去过,这只是一个工具的build议,这个工具在这个日子里挣扎了好几天。