What i am doing here is, fetching a URL which has authentication. Hence, i use the function
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
When it faces authentication, i present a UIAlertView to enter the username and password and if user has entered it correctly, this method is called.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
in this method, i make the login window disappear and bring in the detail view.
Problem arose, when i wanted a logout functionality. All i want is to remove the credentials entered by the user and fetch that URL again, for authentication= purpose. So, i call the didReceiveAuthenticationChallenge.
But what happens is it directly goes to the didReceiveResponse method without asking anything. The problem here is that i am not able to clear the credentials. Can you help me in doing this?
Thanks a lot in advance!
Try Code for Clear cookie of Request
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:@"twitter"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
I know this is an old question, but I have the answer here:
It turns out that cookies aren't the only way that UIWebView stores data. There's also this persistent thing called NSURLCredentialStorage and the only way to clear it is thus:
NSLog(@"Logging out...");
// Clear credential storage
NSURLCredentialStorage *credentialStorage = [NSURLCredentialStorage sharedCredentialStorage];
NSDictionary *credentialProtectionSpaces = [credentialStorage allCredentials];
for (NSURLProtectionSpace *protectionSpace in credentialProtectionSpaces)
{
NSDictionary *credentials = [credentialStorage credentialsForProtectionSpace:protectionSpace];
for (NSString * username in credentials)
{
[credentialStorage removeCredential:[credentials objectForKey:username] forProtectionSpace:protectionSpace];
NSLog(@"clearing: %@", username);
}
}
NSLog(@"checking...");
credentialStorage = [NSURLCredentialStorage sharedCredentialStorage];
credentialProtectionSpaces = [credentialStorage allCredentials];
for (NSURLProtectionSpace *protectionSpace in credentialProtectionSpaces)
{
NSDictionary *credentials = [credentialStorage credentialsForProtectionSpace:protectionSpace];
for (NSString * username in credentials)
{
[credentialStorage removeCredential:[credentials objectForKey:username] forProtectionSpace:protectionSpace];
NSLog(@"checking: %@", username);
}
}
You''ll find that the usernames show the first time, but don't show when checking the second time through the same loop. They've been deleted from the NSURLProtectionSpaces.
-Sean
Great question, and in my case I couldn't figure out why we couldn't log out of a web view.
I used some code from the first answer, but wanted to delete all the cookies in the whole thing rather than just ones associated with a certain string or URL. Here's what I did:
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieJar cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
And this worked! Now when you logout it goes back to the original login screen every time.