【问题标题】:UIWebview didReceiveAuthenticationChallengeUIWebview didReceiveAuthenticationChallenge
【发布时间】:2012-10-03 05:11:19
【问题描述】:

我正在使用 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];
}}

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

谢谢。

【问题讨论】:

    标签: ios xcode nsurlconnection nsurlrequest


    【解决方案1】:

    我刚刚找到它。只需要再添加 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];
    }
    }
    

    谢谢。 :)

    【讨论】:

    • 我有同样的问题,但是这个函数从来没有被调用过:'(我不明白为什么!我已经添加了this.webview.delegate = XYZ。:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多