从沙盒里删除文件, 清除缓存

自己做音乐播放器, 需要清除缓存 我这么做得:

首先, 你要知道缓存的路径, 我的是在网络请求时,直接把音乐缓存到本地

</pre><pre name="code" class="objc"><span style="font-size:18px;"> NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSFileManager *fileManager = [NSFileManager defaultManager];NSString *cacheDirectory = [docPath stringByAppendingPathComponent:@"myCaches"];if (![fileManager isExecutableFileAtPath:@"myCaches"]) {[fileManager createDirectoryAtPath:cacheDirectory withIntermediateDirectories:YES attributes:nil error:nil];}NSString *strPath = [NSString stringWithFormat:@"%ld.aa", [str hash]];NSString *path = [cacheDirectory stringByAppendingPathComponent:strPath];NSLog(@"%@",path);</span>path 就是我的缓存路径

在清除缓存的时候

获取Cache目录路径:(我这获取的时cache里的, 因为存在这里了, 把第一个NSCachesDirectory参数改掉 , 就能获得其他文件夹, 自己的路径后边要拼接字符串)<span style="font-size:18px;">NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);NSString *cachesDir = [[paths objectAtIndex:0] stringByAppendingString:@"/myCaches"];NSLog(@"+++++++++++%@",cachesDir);</span>

获取cachesDir后 要用文件操作,我就直接用这个方法删除了 不知道对不对?

NSFileManager *Manager = [NSFileManager defaultManager];[Manager removeItemAtPath:cachesDir error:nil];

,己欲立先立人,已欲达先达人。

从沙盒里删除文件, 清除缓存

相关文章:

你感兴趣的文章:

标签云: