NSURLConnection: Change HTTP Basic Auth Credentials
NSURLConnection: Change HTTP Basic Auth Credentials
- Subject: NSURLConnection: Change HTTP Basic Auth Credentials
- From: "Johannes Fahrenkrug" <email@hidden>
- Date: Thu, 20 Nov 2008 15:38:24 +0100
Hi,
I'm working on a piece of code that connects to a RESTful webservice
and authenticates via HTTP basic. This works great, I get the
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge
*)challenge
message, provide my credentials and get a successful response from the
server. So far so good.
BUT...
I want to be able to change the credentials while the app is running.
But when after the first successful didReceiveAuthenticationChallenge
call, my delegate never receives that message again.
This is my code:
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge
*)challenge {
#ifdef DEBUG
NSLog(@"got auth challange");
#endif
if ([challenge previousFailureCount] == 0) {
[[challenge sender] useCredential:[NSURLCredential
credentialWithUser:[usernameTextField text]
password:[passwordTextField text]
persistence:NSURLCredentialPersistenceNone]
forAuthenticationChallenge:challenge];
} else {
// this indicates that the connection failed because of bad credentials
authFailed = YES;
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
I've even tried setting persistence to
NSURLCredentialPersistenceForSession and programmatically reset the
stored NSURLCredentials before each request:
// reset the credentials cache...
NSDictionary *credentialsDict = [[NSURLCredentialStorage
sharedCredentialStorage] allCredentials];
if ([credentialsDict count] > 0) {
// the credentialsDict has NSURLProtectionSpace objs as keys and
dicts of userName => NSURLCredential
NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator];
id urlProtectionSpace;
// iterate over all NSURLProtectionSpaces
while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) {
NSEnumerator *userNameEnumerator = [[credentialsDict
objectForKey:urlProtectionSpace] keyEnumerator];
id userName;
// iterate over all usernames for this protectionspace, which are
the keys for the actual NSURLCredentials
while (userName = [userNameEnumerator nextObject]) {
NSURLCredential *cred = [[credentialsDict
objectForKey:urlProtectionSpace] objectForKey:userName];
NSLog(@"cred to be removed: %@", cred);
[[NSURLCredentialStorage sharedCredentialStorage]
removeCredential:cred forProtectionSpace:urlProtectionSpace];
}
}
}
But that didn't help either.
SO: my question is: how do I force an NSURLConnection to send my
delegate the didReceiveAuthenticationChallenge message so I can change
my credentials?
Thanks!
- Johannes
--
http://blog.springenwerk.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden