Clear the credentials from UIWebView

佐手、 提交于 2019-12-06 03:49:57

问题


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!


回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/7256808/clear-the-credentials-from-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!