由於您的問題的一些關鍵部分丟失(如您使用的認證方法),我只是在這裏發佈一個方便的代碼片段,可以解決您的問題。
但請注意:這將刪除所有的cookie和所有緩存的響應以及所有NSURLCredentials(只要它們沒有被保留)。
- (void)removeAllStoredCredentials
{
// Delete any cached URLrequests!
NSURLCache *sharedCache = [NSURLCache sharedURLCache];
[sharedCache removeAllCachedResponses];
// Also delete all stored cookies!
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStorage cookies];
id cookie;
for (cookie in cookies) {
[cookieStorage deleteCookie:cookie];
}
NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
if ([credentialsDict count] > 0) {
// the credentialsDict has NSURLProtectionSpace objs as keys and dicts of userName => NSURLCredential
NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator];
id urlProtectionSpace;
// iterate over all NSURLProtectionSpaces
while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) {
NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator];
id userName;
// iterate over all usernames for this protectionspace, which are the keys for the actual NSURLCredentials
while (userName = [userNameEnumerator nextObject]) {
NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userName];
//NSLog(@"credentials to be removed: %@", cred);
[[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace];
}
}
}
}
在哪裏登錄邏輯?您如何在應用程序中保存登錄信息? – 2015-02-24 11:51:10
爲什麼要低級別和NSURLCache混亂,而不是使用你的圖書館自己的高級機制?你不能只是做一些像[[Digits sharedInstance] logOut]'? – Clafou 2015-02-24 13:15:11
非常感謝! @ Clafou,對我來說絕對完美。 – Daljeet 2015-02-24 13:34:58