ios - 在请求时使用 NSURLCredentialPersistenceForSession 然后在注销时清除凭据仍然保留凭据

标签 ios ipad

登录时,我在 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/

相关文章:

iphone - iPhone 和 iPad 之间的核心数据同步

ios - Pod安装Realm 3.1.1失败

ios - 在不同线程上访问 NSManagedObject 的 objectID?

ios - 冗余二进制文件上传。在 Softwareassets 上已经有构建版本为“1' for train ' 1.1.2”的二进制上传

iphone - UIScrollView 不会滚动

iPhone/iPad 背景图片

iphone - iPad上的“可执行文件已使用无效的权利签名”,在iPhone上运行良好

ios - 无法对自定义对象数组进行排序

iphone - 这些设备使用什么时间间隔对每个触摸坐标进行采样?

iphone - 带有底部填充的 UITextView