[英]How to clean cache on iOS 9 and iOS 10
I want to clean cache on all kinds of version's iOS,however, it only work on iOS 7 and iOS 8 . 我想清除所有版本的iOS上的缓存,但是, 它仅适用于iOS 7和iOS 8 。 On iOS 9 and iOS 10,the same code didn't work,this make me very confused.
在iOS 9和iOS 10上,相同的代码无法正常工作,这让我非常困惑。
In order to find the reason,I create a new demo with the same clean cache method and run on iOS 10,everything goes well,and cleaned cache successfully.So I think,the code I use to clean cache is no problem. 为了找到原因,我使用相同的清理缓存方法创建了一个新演示,并在iOS 10上运行,一切正常,并成功清理了缓存。因此,我认为,用于清理缓存的代码没有问题。 But why it didn't work on my own project when run on iOS 9 and iOS 10?
但是,为什么在iOS 9和iOS 10上运行时,它在我自己的项目上不起作用?
For the sake of confirming whether the code has been calling when run on iOS 9 and iOS 10,I add a breakpoint in this method,it stopped at there.Method had been called,but didn't clean cache. 为了确认在iOS 9和iOS 10上运行时代码是否一直在调用,我在此方法中添加了一个断点,该方法在该处停止。该方法已被调用,但未清除缓存。 The method code I use to clean cache is:
我用来清除缓存的方法代码是:
-(void)removeCache
{
//===============清除缓存==============
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
// NSLog(@"文件数 :%lu",(unsigned long)[files count]);
for (NSString *p in files)
{
NSError *error;
NSString *path = [cachePath stringByAppendingPathComponent:p];
if([[NSFileManager defaultManager] fileExistsAtPath:path])
{
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
}
}
}
I really need some help,this issue had confused me 3 day. 我真的需要一些帮助,这个问题使我困惑了3天。
It seems that iOS 9 and iOS 10 store some files in the Caches folder that your app isn't allowed to delete. 似乎iOS 9和iOS 10在不允许您的应用删除的Caches文件夹中存储了一些文件。
You can use the NSFileManager isDeletableFileAtPath:
method to check. 您可以使用
NSFileManager isDeletableFileAtPath:
方法进行检查。
-(void)removeCache
{
//===============清除缓存==============
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
// NSLog(@"文件数 :%lu",(unsigned long)[files count]);
for (NSString *p in files)
{
NSError *error;
NSString *path = [cachePath stringByAppendingPathComponent:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path] && [[NSFileManager defaultManager] isDeletableFileAtPath:path])
{
if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) {
NSLog(@"Error trying to delete %@: %@", path, error);
}
} else {
NSLog(@"Can't delete %@", path);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.