SandBox,沙盒機(jī)制,是一種安全體系。我們所開(kāi)發(fā)的每一個(gè)應(yīng)用程序在設(shè)備上會(huì)有一個(gè)對(duì)應(yīng)的沙盒文件夾,當(dāng)前的程序只能在自己的沙盒文件夾中讀取文件,不能訪(fǎng)問(wèn)其他應(yīng)用程序的沙盒。在項(xiàng)目中添加的所有非代碼的資源,比如圖片、聲音、屬性列表等都存在自己的沙盒中。此外,在程序運(yùn)行中動(dòng)態(tài)生成的或者從網(wǎng)絡(luò)獲取的數(shù)據(jù),如果要存儲(chǔ),也都是存儲(chǔ)到沙盒中。
沙盒中的默認(rèn)文件夾
(1)Documents:蘋(píng)果建議將程序中建立的或在程序中瀏覽到的文件數(shù)據(jù)保存在該目錄下,iTunes備份和恢復(fù)的時(shí)候會(huì)包括此目錄
(2)Library:存儲(chǔ)程序的默認(rèn)設(shè)置或其它狀態(tài)信息;
里面又包含兩個(gè)文件夾Caches和PReference;
Caches,存放緩存文件,iTunes不會(huì)備份此目錄
(3)tmp:提供一個(gè)即時(shí)創(chuàng)建臨時(shí)文件的地方
獲取沙盒中的不同目錄
代碼
// JRSandBoxPath.h// Fmdb//// Created by jerei on 15-10-30.// Copyright (c) 2015年 jerei. All rights reserved.//#import <Foundation/Foundation.h>@interface JRSandBoxPath: NSObject// 獲取沙盒Document的文件目錄+ (NSString *)getDocumentDirectory;// 獲取沙盒Library的文件目錄+ (NSString *)getLibraryDirectory;// 獲取沙盒Library/Caches的文件目錄+ (NSString *)getCachesDirectory;// 獲取沙盒Preference的文件目錄+ (NSString *)getPreferencePanesDirectory;// 獲取沙盒tmp的文件目錄+ (NSString *)getTmpDirectory;@end//// JRSandBoxPath.m// Fmdb//// Created by jerei on 15-10-30.// Copyright (c) 2015年 jerei. All rights reserved.//#import " JRSandBoxPath.h"@implementation JRSandBoxPath#pragma mark - 獲取沙盒Document的文件目錄+ (NSString *)getDocumentDirectory{ return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];}#pragma mark - 獲取沙盒Library的文件目錄+ (NSString *)getLibraryDirectory{ return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];}#pragma mark - 獲取沙盒Library/Caches的文件目錄+ (NSString *)getCachesDirectory{ return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];}#pragma mark - 獲取沙盒Preference的文件目錄+ (NSString *)getPreferencePanesDirectory{ return [NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES) lastObject];}#pragma mark - 獲取沙盒tmp的文件目錄+ (NSString *)getTmpDirectory{ return NSTemporaryDirectory();}@end
清除緩存
在開(kāi)發(fā)的過(guò)程中,遇到有用的數(shù)據(jù),會(huì)進(jìn)行緩存,當(dāng)該數(shù)據(jù)不需要時(shí),可以清除。在這里整理了幾個(gè)方法,統(tǒng)計(jì)問(wèn)價(jià)的大小,清除指定文件,清除指定目錄下的全部文件等。
代碼
// JRCleanCaches.h// Fmdb//// Created by jerei on 15-10-30.// Copyright (c) 2015年 jerei. All rights reserved.//#import <Foundation/Foundation.h>@interface JRCleanCaches : NSObject// 根據(jù)路徑返回目錄或文件的大小+ (double)sizeWithFilePath:(NSString *)path;// 得到指定目錄下的所有文件+ (NSArray *)getAllFileNames:(NSString *)dirPath;// 刪除指定目錄或文件+ (BOOL)clearCachesWithFilePath:(NSString *)path;// 清空指定目錄下文件+ (BOOL)clearCachesFromDirectoryPath:(NSString *)dirPath;@end//// JRCleanCaches.m// Fmdb//// Created by jerei on 15-10-30.// Copyright (c) 2015年 jerei. All rights reserved.//#import "JRCleanCaches.h"@implementation JRCleanCaches#pragma mark - 根據(jù)路徑返回目錄或文件的大小+ (double)sizeWithFilePath:(NSString *)path{ // 1.獲得文件夾管理者 NSFileManager *manger = [NSFileManager defaultManager]; // 2.檢測(cè)路徑的合理性 BOOL dir = NO; BOOL exits = [manger fileExistsAtPath:path isDirectory:&dir]; if (!exits) return 0; // 3.判斷是否為文件夾 if (dir) { // 文件夾, 遍歷文件夾里面的所有文件 // 這個(gè)方法能獲得這個(gè)文件夾下面的所有子路徑(直接/間接子路徑) NSArray *subpaths = [manger subpathsAtPath:path]; int totalSize = 0; for (NSString *subpath in subpaths) { NSString *fullsubpath = [path stringByAppendingPathComponent:subpath]; BOOL dir = NO; [manger fileExistsAtPath:fullsubpath isDirectory:&dir]; if (!dir) { // 子路徑是個(gè)文件 NSDictionary *attrs = [manger attributesOfItemAtPath:fullsubpath error:nil]; totalSize += [attrs[NSFileSize] intValue]; } } return totalSize / (1024 * 1024.0); } else { // 文件 NSDictionary *attrs = [manger attributesOfItemAtPath:path error:nil]; return [attrs[NSFileSize] intValue] / (1024.0 * 1024.0); }}#pragma mark - 得到指定目錄下的所有文件+ (NSArray *)getAllFileNames:(NSString *)dirPath{ NSArray *files = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:dirPath error:nil]; return files;}#pragma mark - 刪除指定目錄或文件+ (BOOL)clearCachesWithFilePath:(NSString *)path{ NSFileManager *mgr = [NSFileManager defaultManager]; return [mgr removeItemAtPath:path error:nil];}#pragma mark - 清空指定目錄下文件+ (BOOL)clearCachesFromDirectoryPath:(NSString *)dirPath{ //獲得全部文件數(shù)組 NSArray *fileAry = [JRCleanCaches getAllFileNames:dirPath]; //遍歷數(shù)組 BOOL flag = NO; for (NSString *fileName in fileAry) { NSString *filePath = [dirPath stringByAppendingPathComponent:fileName]; flag = [JRCleanCaches clearCachesWithFilePath:filePath]; if (!flag) break; } return flag;}@end
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注