標籤:lib 恢複 定義 blank return sub with 臨時檔案 for
iOS應用程式只能在為該改程式建立的檔案系統中讀取檔案,不可以去其它地方訪問,此地區被成為沙箱。它包含三個檔案夾:
Documents: 蘋果建議將程式中建立的或在程式中瀏覽到的檔案資料儲存在該目錄下,iTunes備份和恢複的時候會包括此目錄,如使用者資訊等永久性檔案;
Library: 它包含兩個檔案夾 caches 和 preferences
Library/Caches:存放快取檔案,iTunes不會備份此目錄,此目錄下檔案不會在應用退出刪除,片、視頻緩衝;
Library/Preferences:包含應用程式的喜好設定檔案;
Temp:這個目錄用於存放臨時檔案,儲存應用程式再次啟動過程中不需要的資訊。
下面是擷取沙箱路徑的方法:
1、//沙箱的根目錄 NSString *homePath = NSHomeDirectory();
2、//沙箱Documents路徑 NSString *docuPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
3、//沙箱中Library路徑 NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
4、//沙箱中Library/Caches路徑 NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
5、//沙箱中Temp路徑 NSString *tempPath = NSTemporaryDirectory();
//計算檔案夾的大小
-(float)calculateFolderSizeWithPath:(NSString *)path{
NSFileManager *fileManager=[NSFileManager defaultManager];
NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
cachePath=[cachePath stringByAppendingPathComponent:path];
CGFloat folderSize=0;
if ([fileManager fileExistsAtPath:cachePath])
{
NSArray *childFiles=[fileManager subpathsAtPath:cachePath];
for (NSString *fileName in childFiles)
{
NSString *fileAbsolutePath=[cachePath stringByAppendingPathComponent:fileName];
CGFloat size=[self calculateFileSizeWithPath:fileAbsolutePath];
folderSize += size;
NSLog(@"fileAbsolutePath=%@",fileAbsolutePath);
}
//SDWebImage架構自身計算緩衝
folderSize+=[[SDImageCache sharedImageCache] getSize];
return folderSize/1024.0/1024.0;
}
return 0;
}
//計算單個檔案大小
-(CGFloat)calculateFileSizeWithPath:(NSString *)path{
NSFileManager *fileManager=[NSFileManager defaultManager];
if([fileManager fileExistsAtPath:path]){
CGFloat size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
return size;
}
return 0;
}
- (void)clearCacheClick
{
NSArray *arr_Path = @[@"/Media",@"/Music",@"/Voice"];
[[UIView alloc]showHUDWithTitle:@"正在清除緩衝···" WithState:1];
//一是清除SDWebImage的緩衝,二是清除自訂檔案快取
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
for (NSString *pathName in arr_Path) {
NSString *fileAbsolutePath = [documentDir stringByAppendingPathComponent:pathName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:fileAbsolutePath]) {
[fileManager removeItemAtPath:fileAbsolutePath error:nil];
[fileManager createDirectoryAtPath:fileAbsolutePath withIntermediateDirectories:YES attributes:nil error:nil];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[[UIView alloc]showAndHideHUDWithTitle:@"清除完成" WithState:0];
// 設定文字
self.lable_Cache.text = @"0.00 MB";
});
});
}];
}
iOS 清除緩衝