连续NSURLConnection请求失败身份验证

时间:2015-02-20 20:43:40

标签: ios authentication nsurlconnection

这似乎只是iOS8的一个问题。

我有一个使用NSURLConnection委托的应用。一切都完全正常,除了在iOS8中创建并连续运行多个连接时,任意数量的连接都无法进行身份验证。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if (challenge.previousFailureCount == 0)
    {
        KeychainItemWrapper *itemWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"login" accessGroup:nil];
        [[challenge sender] useCredential:[NSURLCredential credentialWithUser:[itemWrapper objectForKey:(__bridge id)(kSecAttrAccount)] password:[itemWrapper objectForKey:(__bridge id)(kSecValueData)] persistence:NSURLCredentialPersistenceNone] forAuthenticationChallenge:challenge];
        _authenticationChallenged = YES;
    }


    else
    {
        [self cancel];
        KeychainItemWrapper *itemWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"login" accessGroup:nil];
        if ([[itemWrapper objectForKey:(__bridge id)(kSecAttrAccount)] isEqualToString:@""]) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Please Log In" message:@"To use this app, please log in" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Log In", nil];
            [alertView setTag:2];
            [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
            [alertView show];
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Invalid Credentials" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Log In", nil];
            [alertView setTag:2];
            [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
            [alertView show];
        }
    }
}

在上面的代码中是问题所在。即使凭据正确并且在每个其他请求上正常工作,也会调用else语句。只有在我同时创建多个(单独的)NSURLConnections并运行它们时才会发生这种情况。

有什么想法吗?

谢谢!

0 个答案:

没有答案