iOS 清除App缓存
//计算缓存大小
- (CGFloat)getSize {
NSUIntegersize =0;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSDirectoryEnumerator*fileEnumerator = [fileManagerenumeratorAtPath:cachePath];
for(NSString*fileNameinfileEnumerator) {
NSString*filePath = [cachePathstringByAppendingPathComponent:fileName];
NSDictionary*attrs = [fileManagerattributesOfItemAtPath:filePatherror:nil];
size += [attrsfileSize];
}
//将大小转化为M
returnsize /1024.0/1024.0;
}
/*
12-23 update: use removeItemAtPath to delete each subdir
instead of delete the root dir and then recreate it
*/
- (void)clearFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"%@",cachePath);
NSDirectoryEnumerator*fileEnumerator = [fileManagerenumeratorAtPath:cachePath];
for(NSString*fileNameinfileEnumerator) {
NSString*filePath = [cachePathstringByAppendingPathComponent:fileName];
[fileManagerremoveItemAtPath:filePatherror:nil];
}
}