【发布时间】:2017-02-11 06:10:25
【问题描述】:
我正在尝试清除 iOS 10 中的缓存。但 removeAllCachedResponses 无法正常工作。 removeAllCachedResponses 在 iOS10 中是否损坏。因为这适用于 iOS 9。
【问题讨论】:
-
你看到这个answer了吗?
标签: ios ios10 clear-cache
我正在尝试清除 iOS 10 中的缓存。但 removeAllCachedResponses 无法正常工作。 removeAllCachedResponses 在 iOS10 中是否损坏。因为这适用于 iOS 9。
【问题讨论】:
标签: ios ios10 clear-cache
你说的是这个吗?
[[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
}];
【讨论】:
如果您想从应用程序目录中删除文件。 File from Directory的路径有很多种,下面是其中一种。
NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *mainPath = [myPathList objectAtIndex:0];
mainPath = [mainPath stringByAppendingPathComponent:DirectoryName];
现在您可以删除此路径下的文件,mainPath 是文件的完整路径。
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]);
}
【讨论】: