【问题标题】:How to clear cache in iOS10?iOS10如何清除缓存?
【发布时间】:2017-02-11 06:10:25
【问题描述】:

我正在尝试清除 iOS 10 中的缓存。但 removeAllCachedResponses 无法正常工作。 removeAllCachedResponses 在 iOS10 中是否损坏。因为这适用于 iOS 9。

【问题讨论】:

  • 你看到这个answer了吗?

标签: ios ios10 clear-cache


【解决方案1】:

你说的是这个吗?

[[NSURLCache sharedURLCache] removeAllCachedResponses];

您还可以修改请求的缓存行为以选择性地缓存响应。如果您偶然使用AFNetworking,则可以使用setCacheResponseBlock。例如。在一个项目中,我将其设置为为所有大型视频和音频文件返回nil。但允许它缓存较小的图像文件。

[streamingOperation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
    return nil; // Ensures we are not unecessarily caching asset data to Cache.db
}];

【讨论】:

  • 我的应用程序需要清除所有缓存响应。
【解决方案2】:

如果您想从应用程序目录中删除文件。 File from Directory的路径有很多种,下面是其中一种。

NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *mainPath    = [myPathList  objectAtIndex:0];

mainPath = [mainPath stringByAppendingPathComponent:DirectoryName];

现在您可以删除此路径下的文件,ma​​inPath 是文件的完整路径。

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:mainPath];

if (fileExists)
{
     BOOL success = [fileManager removeItemAtPath:mainPath error:&error];
     if (!success) NSLog(@"Error: %@", [error localizedDescription]);

}

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 2011-05-10
    • 2011-11-04
    • 2020-03-13
    • 2013-02-09
    • 2011-10-17
    • 2011-02-01
    • 2015-11-02
    • 2014-05-26
    相关资源
    最近更新 更多