Re: NSURLConnection: Change HTTP Basic Auth Credentials
Re: NSURLConnection: Change HTTP Basic Auth Credentials
- Subject: Re: NSURLConnection: Change HTTP Basic Auth Credentials
- From: "Johannes Fahrenkrug" <email@hidden>
- Date: Fri, 21 Nov 2008 13:09:03 +0100
Hi again,
after much wailing and gnashing of teeth, I think I've figured it out.
The solution is adding a # to the end of the URL:
http://blog.springenwerk.com/2008/11/i-am-currently-building-iphone.html
I'm pretty sure this is a bug, though.
- Johannes
On Thu, Nov 20, 2008 at 3:38 PM, Johannes Fahrenkrug
<email@hidden> wrote:
> 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
>
--
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