In the app I'm developing I connect to a server, to request some data. This works fine. An authentication challenge is received and processed, and the connection performs beautifully. If however I then change the username/password, and attempt to connect again, the authentication is refused by the server, but only when running on the device (iPhone/iPod Touch); when running in the simulator changing to another account works just fine. This problem persists between subsequent runs of the app. The only way to be able to change to a new account on the device, is to delete the app completely from the device, and then install it anew. In the simulator however, I can change accounts and successfully connect to the sever as often as I want, including within the same app session.
I verified the password and username are always correct when authenticating.
This is how I handle the challenge:
=====================
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:userEmailAddressKey];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:uUserPasswordKey];
NSInteger failureCount = challenge.previousFailureCount;
if (failureCount == 0 && username && password) {
self.credential = [NSURLCredential credentialWithUser:username
password:password
persistence:NSURLCredentialPersistenceForSession];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:self.credential
forProtectionSpace:[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] myProtectionSpace]];
[[challenge sender] useCredential:self.credentialforAuthenticationChallenge:challenge];
} else {
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
}
}
=====================
The protection space is created when the app starts:
=====================
self.myProtectionSpace = [[[ NSURLProtectionSpacealloc] initWithHost:@"www.some.url"
port:80
protocol:@"http"
realm:nil
authenticationMethod:NSURLAuthenticationMethodDefault] autorelease];
=====================
The connection is created thus:
=====================
-(void)fetchURL
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURLURLWithString:self.urlString]];
switch (method) {
case GET:
[theRequest setHTTPMethod:@"GET"];
break;
case POST:
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:[NSString stringWithFormat:@"%d", [self .postDatalength]] forHTTPHeaderField:@"Content-Length"];
[theRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody:self.postData];
break;
}
[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[[NSRunLoop currentRunLoop ] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:60]];
[pool release];
}
=====================
Any ideas as to why I can't change accounts on the device, not even after exiting and restarting the app? Since the code works in the simulator, and I can't myself find anything wrong with it, it looks like a bug in the iPhone OS, which I should file with Apple. Is anyone familiar with an issue like this, and/or does anyone have a workaround so that we can change accounts without having to delete and re-install the app?
-António
-----------------------------------------------------------
And could you keep your heart in wonder
at the daily miracles of your life,
your pain would not seem less wondrous
than your joy.
--Kahlil Gibran
-----------------------------------------------------------
----------------------------------------------------
There is a world of difference between
searching for happiness and choosing
to be happy.
----------------------------------------------------
_______________________________________________
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