Created
February 1, 2017 06:17
-
-
Save vineetchoudhary/3a11fa133745583dff658c65f31aa755 to your computer and use it in GitHub Desktop.
Clear cache objective-c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (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]; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment