Solved!
It turns out that NSURLConnection
was actually behaving correctly - that is, didReceiveAuthenticationChallenge:
was being called for every authentication challenge.
The problem was that the server was not sending challenges after the first one. This turned out to be because the server was setting a cookie.
You can force a new challenge by simply deleting the cookie. Because there are no other useful cookies for this server, I just delete all of them:
- (void)clearCookiesForURL {
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStorage cookiesForURL:_URL];
for (NSHTTPCookie *cookie in cookies) {
NSLog(@"Deleting cookie for domain: %@", [cookie domain]);
[cookieStorage deleteCookie:cookie];
}
}