5

我在这里做的是,获取一个具有身份验证的 URL。因此,我使用该功能

  - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

当它面临身份验证时,我提供一个 UIAlertView 来输入用户名和密码,如果用户输入正确,则调用此方法。

  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

在这种方法中,我使登录窗口消失并进入详细视图。

当我想要注销功能时,问题就出现了。我想要的只是删除用户输入的凭据并再次获取该 URL,用于身份验证 = 目的。所以,我打电话给didReceiveAuthenticationChallenge

但是发生的情况是它直接进入didReceiveResponse方法而不询问任何内容。这里的问题是我无法清除凭据。你能帮我做这件事吗?

提前非常感谢!

4

3 回答 3

7

尝试清除请求 cookie 的代码

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];
    }
}
于 2011-09-01T09:29:46.753 回答
3

我知道这是一个老问题,但我在这里有答案:

事实证明,cookie 并不是 UIWebView 存储数据的唯一方式。还有一个叫做 NSURLCredentialStorage 的持久性东西,清除它的唯一方法是:

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

您会发现用户名第一次显示,但在第二次通过同一循环检查时不显示。它们已从 NSURLProtectionSpaces 中删除。

-肖恩

于 2013-08-09T09:46:21.210 回答
3

很好的问题,就我而言,我无法弄清楚为什么我们无法退出网络视图。

我使用了第一个答案中的一些代码,但想删除整个事情中的所有 cookie,而不仅仅是与某个字符串或 URL 关联的那些。这是我所做的:

NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (NSHTTPCookie *cookie in [cookieJar cookies]) {
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}

这行得通!现在,当您注销时,它每次都会返回原始登录屏幕。

于 2014-02-21T16:19:01.573 回答