登录时,我在 NSURLConnection
委托(delegate)方法的 didReceiveAuthenticationChallenge
中使用 NSURLCredentialPersistenceForSession
。
现在,当我注销并使用此代码清除存储时..
NSURLCredentialStorage *credentialStorage = [NSURLCredentialStorage sharedCredentialStorage];
NSDictionary *credentialsDicationary = [credentialStorage allCredentials];
NSLog(@"credentialsDicationary..%@",[credentialsDicationary description]);
for (NSURLProtectionSpace *space in [credentialsDicationary allKeys]) {
NSDictionary *spaceDictionary = [credentialsDicationary objectForKey:space];
NSLog(@"spaceDictionary..%@",[spaceDictionary description]);
for (id userName in [spaceDictionary allKeys]) {
NSURLCredential *credential = [spaceDictionary objectForKey:userName];
[credentialStorage removeCredential:credential forProtectionSpace:space];
}
}
但是当我在注销后突然再次登录时,登录发生了错误的凭据。请让mw知道如何清除缓存。如果我在大约 5 秒后重新登录,它会起作用。
提前致谢... 阿杰
最佳答案
如果您使用的是 NSURLSession,请使 session 无效并创建一个新 session ,例如:
[self.session invalidateAndCancel];
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
这将清除 session 范围的凭据。
关于ios - 在请求时使用 NSURLCredentialPersistenceForSession 然后在注销时清除凭据仍然保留凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904601/