iOS 獲取沙盒文件路徑及 寫入/刪除 沙盒文件

轉(zhuǎn)自:
作者:李sir35
鏈接:http://www.itdecent.cn/p/0dbe875d7723
來源:簡書

前言:

出于安全考慮,iOS系統(tǒng)的沙盒機制規(guī)定每個應(yīng)用都只能訪問當(dāng)前沙盒目錄下面的文件(也有例外,比如系統(tǒng)通訊錄能在用戶授權(quán)的情況下被第三方應(yīng)用訪問),這個規(guī)則把iOS系統(tǒng)的封閉性展現(xiàn)的淋漓盡致。

一、沙盒中幾個主要的目錄

每個沙盒下面都有相似的目錄結(jié)構(gòu),如下圖所示(出自蘋果官方文檔):

image

每個應(yīng)用的沙盒目錄都是相似的,主要包含圖中所示的4個目錄:

1、MyApp.app

①存放內(nèi)容

該目錄包含了應(yīng)用程序本身的數(shù)據(jù),包括資源文件和可執(zhí)行文件等。程序啟動以后,會根據(jù)需要從該目錄中動態(tài)加載代碼或資源到內(nèi)存,這里用到了lazy loading的思想。

②整個目錄是只讀的

為了防止被篡改,應(yīng)用在安裝的時候會將該目錄簽名。非越獄情況下,該目錄中內(nèi)容是無法更改的;在越獄設(shè)備上如果更改了目錄內(nèi)容,對應(yīng)的簽名就會被改變,這種情況下蘋果官網(wǎng)描述的后果是應(yīng)用程序?qū)o法啟動,我沒實踐過。

③是否會被iTunes同步

2、Documents

①存放內(nèi)容

我們可以將應(yīng)用程序的數(shù)據(jù)文件保存在該目錄下。不過這些數(shù)據(jù)類型僅限于不可再生的數(shù)據(jù),可再生的數(shù)據(jù)文件應(yīng)該存放在Library/Cache目錄下。

②是否會被iTunes同步

3、Documents/Inbox

①存放內(nèi)容

該目錄用來保存由外部應(yīng)用請求當(dāng)前應(yīng)用程序打開的文件。

比如我們的應(yīng)用叫A,向系統(tǒng)注冊了幾種可打開的文件格式,B應(yīng)用有一個A支持的格式的文件F,并且申請調(diào)用A打開F。由于F當(dāng)前是在B應(yīng)用的沙盒中,我們知道,沙盒機制是不允許A訪問B沙盒中的文件,因此蘋果的解決方案是講F拷貝一份到A應(yīng)用的Documents/Inbox目錄下,再讓A打開F。

②是否會被iTunes同步

4、Library

①存放內(nèi)容

蘋果建議用來存放默認設(shè)置或其它狀態(tài)信息。

②是否會被iTunes同步

是,但是要除了Caches子目錄外

5、Library/Caches

①存放內(nèi)容

主要是緩存文件,用戶使用過程中緩存都可以保存在這個目錄中。前面說過,Documents目錄用于保存不可再生的文件,那么這個目錄就用于保存那些可再生的文件,比如網(wǎng)絡(luò)請求的數(shù)據(jù)。鑒于此,應(yīng)用程序通常還需要負責(zé)刪除這些文件。

②是否會被iTunes同步

否。

6、Library/Preferences

①存放內(nèi)容

應(yīng)用程序的偏好設(shè)置文件。我們使用NSUserDefaults寫的設(shè)置數(shù)據(jù)都會保存到該目錄下的一個plist文件中,這就是所謂的寫道plist中!

②是否會被iTunes同步

7、tmp

①存放內(nèi)容

各種臨時文件,保存應(yīng)用再次啟動時不需要的文件。而且,當(dāng)應(yīng)用不再需要這些文件時應(yīng)該主動將其刪除,因為該目錄下的東西隨時有可能被系統(tǒng)清理掉,目前已知的一種可能清理的原因是系統(tǒng)磁盤存儲空間不足的時候。

②是否會被iTunes同步

//獲取Documents路徑


NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString*path=[paths objectAtIndex:0];

NSLog(@"path:%@",path);

//獲取Library路徑

NSArray*paths=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);

NSString*path=[paths objectAtIndex:0];

NSLog(@"path:%@",path);

//獲取Caches路徑

NSArray*paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);

NSString*path=[paths objectAtIndex:0];

NSLog(@"path:%@",path);

//獲取tmp路徑

NSString*tmp=NSTemporaryDirectory();

NSLog(@"tmp:%@",tmp);

demo

// 把 圖片 寫入 沙盒

-(void)photoFile {

//此處首先指定了圖片存取路徑(默認寫到應(yīng)用程序沙盒 中)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

//并給文件起個文件名

NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin"];

BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];

if (blHave) {

NSLog(@"already have");

return ;

}

//此處的方法是將圖片寫到Documents文件中 如果寫入成功會彈出一個警告框,提示圖片保存成功

NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"pin" ofType:@"png"];

NSData *data = [NSData dataWithContentsOfFile:strPathOld];

BOOL result = [data writeToFile:uniquePath atomically:YES];

if (result) {

NSLog(@"success");

}else {

NSLog(@"no success");

}

}

// 刪除沙盒里的文件

-(void)deleteFile {

NSFileManager* fileManager=[NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

//文件名

NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin.png"];

BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];

if (!blHave) {

NSLog(@"no  have");

return ;

}else {

NSLog(@" have");

BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];

if (blDele) {

NSLog(@"dele success");

}else {

NSLog(@"dele fail");

}

}

}

//向沙盒里 寫入文件夾,并向文件夾里 寫入東西

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *folder = [document stringByAppendingPathComponent:@"folder"];

NSString *filePath = [folder stringByAppendingPathComponent:@"test.png"];

if (![fileManager fileExistsAtPath:folder]) {

BOOL blCreateFolder= [fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:NULL];

if (blCreateFolder) {

NSLog(@" folder success");

}else {

NSLog(@" folder fial");

}

}else {

NSLog(@" 沙盒文件已經(jīng)存在");

}

if (![fileManager fileExistsAtPath:filePath]) {

NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];

NSData *data = [NSData dataWithContentsOfFile:strPathOld];

BOOL result = [data writeToFile:filePath atomically:YES];

if (result) {

NSLog(@"success");

}else {

NSLog(@"no success");

}

}

//得到沙盒文件夾 下的所有文件

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *document=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *folder =[document stringByAppendingPathComponent:@"folder"];

NSArray *fileList ;

fileList =[fileManager contentsOfDirectoryAtPath:folder error:NULL];

for (NSString *file in fileList) {

NSLog(@"file=%@",file);

NSString *path =[folder stringByAppendingPathComponent:file];

NSLog(@"得到的路徑=%@",path);

}

作者:李sir35
鏈接:http://www.itdecent.cn/p/0dbe875d7723
來源:簡書

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容