每個(gè)ios應(yīng)用都有自己的應(yīng)用沙盒,應(yīng)用沙盒就是文件系統(tǒng)目錄,與其他應(yīng)用的文件系統(tǒng)隔離,ios系統(tǒng)不允許訪問(wèn)其他應(yīng)用的應(yīng)用沙盒。在ios8中已經(jīng)開(kāi)放訪問(wèn)。
應(yīng)用沙盒一般包括以下幾個(gè)文件目錄:應(yīng)用程序包、Documents、Libaray(下面有Caches和Preferences目錄)、tmp。
應(yīng)用程序包:包含所有的資源文件和可執(zhí)行文件。
Documents:保存應(yīng)用運(yùn)行時(shí)生成的需要持久化的數(shù)據(jù),iTunes會(huì)自動(dòng)備份該目錄。蘋(píng)果建議將程序中建立的或在程序中瀏覽到的文件數(shù)據(jù)保存在該目錄下,iTunes備份和恢復(fù)的時(shí)候會(huì)包括此目錄
tmp:保存應(yīng)用運(yùn)行時(shí)所需的臨時(shí)數(shù)據(jù),使用完畢后再將相應(yīng)的文件從該目錄刪除。應(yīng)用沒(méi)有運(yùn)行時(shí),系統(tǒng)也有可能會(huì)清除該目錄下的文件,iTunes不會(huì)同步該目錄。iphone重啟時(shí),該目錄下的文件會(huì)丟失。
Library:存儲(chǔ)程序的默認(rèn)設(shè)置和其他狀態(tài)信息,iTunes會(huì)自動(dòng)備份該目錄。
Libaray/Caches:存放緩存文件,iTunes不會(huì)備份此目錄,此目錄下文件不會(huì)在應(yīng)用退出刪除。一般存放體積比較大,不是特別重要的資源。
Libaray/Preferences:保存應(yīng)用的所有偏好設(shè)置,ios的Settings(設(shè)置)應(yīng)用會(huì)在該目錄中查找應(yīng)用的設(shè)置信息,iTunes會(huì)自動(dòng)備份該目錄。
說(shuō)明: 以上內(nèi)容摘抄
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *cacheStr = [NSString stringWithFormat:@"%.2fMB",[self filePath]];
// 顯示緩存大小
self.caceLabel.text = cacheStr;
}
// 返回文件大小
- (float)filePath{
// 獲取文件目錄
NSString *cachPath = [NSSearchPathForDirectoriesInDomains ( NSCachesDirectory, NSUserDomainMask, YES) firstObject];
return [self folderSizeAtPath:cachPath];
}
// 遍歷文件夾獲得文件夾大小, 返回多少 M
- (float) folderSizeAtPath:( NSString *) folderPath{
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:folderPath]){
return 0;
}
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
NSString * fileName;
long long folderSize = 0 ;
while ((fileName = [childFilesEnumerator nextObject]) != nil ){
NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
return folderSize / (1024.0 * 1024.0);
}
- (long long) fileSizeAtPath:( NSString *) filePath{
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath :filePath]){
return [[manager attributesOfItemAtPath:filePath error : nil] fileSize ];
}
return 0;
}
// 按鈕點(diǎn)擊事件 storyboard
- (IBAction)clear:(UIButton *)sender {
[self clearFile];
}
- (void) clearFile{
NSString *cachPath = [NSSearchPathForDirectoriesInDomains ( NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath :cachPath];
for ( NSString *p in files) {
NSError *error = nil ;
NSString *path = [cachPath stringByAppendingPathComponent:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error :&error];
}
}
// 清理完成后刷新界面
[self performSelectorOnMainThread:@selector(clearCachSuccess) withObject:nil waitUntilDone:YES];
}
- (void)clearCachSuccess{
NSString *str = [NSString stringWithFormat:@"%.2fMB",[self filePath]];
self.caceLabel.text = str;
}
鏈接: http://pan.baidu.com/s/1pLKRJuF 密碼: 24mg