Search code examples
iphoneuiwebviewnsurlconnectionnsurlcredential

Clear the credentials from UIWebView


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!


Solution

  • 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];
        }
    }