加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – UIWebview didReceiveAuthenticationChallenge

发布时间:2020-12-14 17:39:35 所属栏目:百科 来源:网络整理
导读:我正在使用webview加载网站.我设法运行应用程序. 但我的问题是,如果我输入错误的密码,我无法加载网站,它只显示一个白色的屏幕. 如果我传递了正确的用户名/密码,它将加载网站. 有没有办法处理我的身份验证用户名/密码是正确还是错误? 我正在使用此代码. - (v
我正在使用webview加载网站.我设法运行应用程序.
但我的问题是,如果我输入错误的密码,我无法加载网站,它只显示一个白色的屏幕.

如果我传递了正确的用户名/密码,它将加载网站.
有没有办法处理我的身份验证用户名/密码是正确还是错误?

我正在使用此代码.

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodNTLM])
{
    NSURLCredential *credentail = [NSURLCredential
                                   credentialWithUser:@"username" 
                                   password:@"Password"
                                   persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge];
}}

上面的代码中包含正确的用户名和密码.如果我将用户名更改为“用户”或密码为“通过”,我无法加载该网站.如何捕获身份验证错误?

谢谢.

解决方法

我刚发现它.只需要再添加1个验证. [[challenge previousFailureCount] == 0.这里是代码.

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
//receive a authenticate and challenge with the user credential
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodNTLM] &&
    [challenge previousFailureCount] == 0)
{
    NSURLCredential *credentail = [NSURLCredential 
                                   credentialWithUser:@"username" 
                                   password:@"password"
                                   persistence:NSURLCredentialPersistenceForSession];


    [[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge];
}
else 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Message" message:@"Invalid credentails" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
}

谢谢.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读