ios - removeAllCachedResponses 不能清除sharedURLCache?

标签 ios objective-c cocoa cocoa-touch

我做了一个按钮来清除我的缓存:

    [[NSURLCache sharedURLCache]removeAllCachedResponses];

完成后,我检查 sharedURLCache 的大小:

NSInteger sizeInteger = [[NSURLCache sharedURLCache] currentDiskUsage];
float sizeInMB = sizeInteger / (1024.0f * 1024.0f);

sizeInMB 为 0.17,有时为 0.13。从不 0.0。为什么 removeAllCachedResponses 不会使 sharedURLCache 为零?

ps: 在 AppDelegate.m 中 didFinishLaunchingWithOptions:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
                                                                    diskCapacity:20 * 1024 * 1024
                                                                        diskPath:nil];

    [NSURLCache setSharedURLCache:sharedCache];

最佳答案

在渗透测试(安全测试)期间,我遇到了 iOS 在 cache.db 中缓存 url 请求/响应的问题。

我试过用

URLCache.shared.removeCachedResponse(用于:URLRequest)和 URLCache.shared.removeAllCachedResponses()

在收到 API 调用的响应后,上述两个 API 似乎都无法正常工作

然后通过在 AppDelegate 的 didFinishLaunchingWithOptions 中添加以下两行代码使其工作

// first remove all url cache from previous versions of app
URLCache.shared.removeAllCachedResponses()


// As we do not want to cache any request response so
// updated shared instance of url cache with custom url cache where
// memoryCapacity = 0, diskCapacity = 0 and diskPath = nil
URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)

关于ios - removeAllCachedResponses 不能清除sharedURLCache?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31803720/

相关文章:

ios - 进入全屏时 iPhone 上的 MPMoviePlayerController 暂停

ios - 是否可以访问 Facebook 当前的热门话题?

iPhone -Pthread 条件信号未解除阻塞等待线程

ios - 如何更改UISegmentedControl的单个索引的字体大小?

c++ - 我如何获得 argv[0]?

ios - 将 subview 缩放到父级大小

IOS 如何使用 RESTFUL API 从 Web 服务中检索数据

cocoa - 将文本字段绑定(bind)到共享用户默认 Controller 不使用空值

objective-c - 要成为 NSDictionary 的键,类必须也实现 isEqual : and hash?

ios - 正确地将 info.plist 条目转换为 NS* 类型