I am trying to delete a NSURLCredential that is written when I access a secure HTTP site. The code below works and deletes the NSURLCredential from NSURLCredentialStorage, but even though I am specifying that:
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
does not reload cached data and I have implemented the method:
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}
the next time I run the application deleteStoredCredential finds a new credential for “loginSiteName.com” and deletes it. My question is, am I missing something, is there anywhere else I need to clear, could the credential be cached on the iPhone?
-(void)deleteStoredCredential {
NSURLCredentialStorage *sharedCredentialStorage = [NSURLCredentialStorage sharedCredentialStorage];
NSDictionary *DICT_allProtectionSpaces = [sharedCredentialStorage allCredentials];
// DICT KEY = NSURLProtectionSpace
// DICT VALUE = NSDictionary
for(NSURLProtectionSpace *thisProtectionSpace in DICT_allProtectionSpaces) {
if([[thisProtectionSpace host] isEqualToString:@"loginSiteName.com"]) {
NSDictionary *DICT_allCredentials = [sharedCredentialStorage credentialsForProtectionSpace:thisProtectionSpace];
// DICT KEY = NSString (i.e. username)
// DICT VALUE = NSURLCredential
for(NSString *thisUserName in DICT_allCredentials) {
NSURLCredential *credentialToDelete = [DICT_allCredentials valueForKey:thisUserName];
NSLog(@"REMOVE UID: %@ WITH PWD: %@", [credentialToDelete user], [credentialToDelete password]);
[sharedCredentialStorage removeCredential:credentialToDelete forProtectionSpace:thisProtectionSpace];
}
}
}
}
I have found the problem: the
NSURLCredentialthat I create to access the site was created withNSURLCredentialPersistencePermanentinstead ofNSURLCredentialPersistenceNone. As a consequence the credential was getting stored in the iPhone keychain.