gpt4 book ai didi

ios - 如何清除 iOS 9 和 iOS 10 上的缓存

转载 作者:行者123 更新时间:2023-11-28 21:19:07 24 4
gpt4 key购买 nike

我想在各种版本的 iOS 上清理缓存,但是,它只适用于 iOS 7 和 iOS 8。在 iOS 9 和 iOS 10 上,相同的代码不起作用,这让我很困惑。

为了找到原因,我用同样的清理缓存方法新建了一个demo,在iOS 10上运行,一切顺利,清理缓存成功。所以我想,我用来清理缓存的代码是没有问题的. 但为什么它在 iOS 9 和 iOS 10 上运行时在我自己的项目上不起​​作用?

为了确认代码在iOS 9和iOS 10上运行时是否一直在调用,我在这个方法中加了一个断点,它停在那里。方法已经被调用,但没有清理缓存。 我用来清理缓存的方法代码是:

-(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];
}
}
}

我真的需要一些帮助,这个问题让我困惑了 3 天。

最佳答案

iOS 9 和 iOS 10 似乎在 Caches 文件夹中存储了一些您的应用程序不允许删除的文件。

可以使用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);
}
}
}

关于ios - 如何清除 iOS 9 和 iOS 10 上的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com