舉例詳解iOS開(kāi)發(fā)過(guò)程中的沙盒機(jī)制與文件
iOS沙盒機(jī)制
iOS應(yīng)用程序只能在為該改程序創(chuàng)建的文件系統(tǒng)中讀取文件,不可以去其它地方訪問(wèn),此區(qū)域被成為沙盒,所以所有的非代碼文件都要保存在此,例如圖像,圖標(biāo),聲音,映像,屬性列表,文本文件等。
- 每個(gè)應(yīng)用程序都有自己的存儲(chǔ)空間
- 應(yīng)用程序不能翻過(guò)自己的圍墻去訪問(wèn)別的存儲(chǔ)空間的內(nèi)容
打開(kāi)模擬器沙盒目錄
方法1、可以設(shè)置顯示隱藏文件,然后在Finder下直接打開(kāi)。設(shè)置查看隱藏文件的方法如下:打開(kāi)終端,輸入命名
<p class="p1">顯示Mac隱藏文件的命令:
隱藏Mac隱藏文件的命令:
現(xiàn)在能看到資源庫(kù)文件夾了。

打開(kāi)資源庫(kù)后找到/Application Support/iPhone Simulator/文件夾。這里面就是模擬器的各個(gè)程序的沙盒目錄了。

方法2、這種方法更方便,在Finder上點(diǎn)->前往 然后按住"option"鍵,就會(huì)出現(xiàn)"資源庫(kù)",其他同上
目錄結(jié)構(gòu)
默認(rèn)情況下,每個(gè)沙盒含有3個(gè)文件夾:Documents, Library 和 tmp。因?yàn)閼?yīng)用的沙盒機(jī)制,應(yīng)用只能在幾個(gè)目錄下讀寫文件
Documents:蘋果建議將程序中建立的或在程序中瀏覽到的文件數(shù)據(jù)保存在該目錄下,iTunes備份和恢復(fù)的時(shí)候會(huì)包括此目錄
Library:存儲(chǔ)程序的默認(rèn)設(shè)置或其它狀態(tài)信息;
Library/Caches:存放緩存文件,iTunes不會(huì)備份此目錄,此目錄下文件不會(huì)在應(yīng)用退出刪除
tmp:提供一個(gè)即時(shí)創(chuàng)建臨時(shí)文件的地方。
iTunes在與iPhone同步時(shí),備份所有的Documents和Library文件。
iPhone在重啟時(shí),會(huì)丟棄所有的tmp文件。
這是上面提到的三個(gè)目錄 :Documents、Library、 tmp

幾個(gè)常用的代碼示例:
1、獲取程序的Home目錄
NSString *homeDirectory = NSHomeDirectory();
NSLog(@"path:%@", homeDirectory);
2、獲取document目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
3、獲取Cache目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"%@", path);
4、獲取Library目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"%@", path);
5、獲取Tmp目錄
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"%@", tmpDir);
6、寫入文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
if (!docDir) {
NSLog(@"Documents 目錄未找到");
}
NSArray *array = [[NSArray alloc] initWithObjects:@"內(nèi)容",@"content",nil];
NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
[array writeToFile:filePath atomically:YES];
7、寫入文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
NSLog(@"%@", array);
8、判斷一個(gè)文件是否存在,傳入全路徑(fileExistsAtPath)
// 創(chuàng)建文件管理器
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * filePath = [documents stringByAppendingPathComponent:@"test"];
// 判斷一個(gè)文件是否存在,傳入全路徑
if ([fileManager fileExistsAtPath:filePath]) {
NSLog(@"it is exit");
}
9、在Documents里創(chuàng)建目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"documentsDirectory%@",documentsDirectory);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
// 創(chuàng)建目錄
[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
10、在目錄下創(chuàng)建文件
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];
NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test22.txt"];
NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test33.txt"];
NSString *string = @"寫入內(nèi)容,write String";
[fileManager createFileAtPath:testPath contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
[fileManager createFileAtPath:testPath2 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
[fileManager createFileAtPath:testPath3 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
11、獲取目錄列里所有文件名
兩種方法獲?。簊ubpathsOfDirectoryAtPath 和subpathsAtPath
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"documentsDirectory%@",documentsDirectory);
NSFileManager *fileManage = [NSFileManager defaultManager];
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];
NSLog(@"%@",file);
NSArray *files = [fileManage subpathsAtPath: myDirectory ];
NSLog(@"%@",files);
12、fileManager使用操作當(dāng)前目錄
//創(chuàng)建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//更改到待操作的目錄下
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
//創(chuàng)建文件fileName文件名稱,contents文件的內(nèi)容,如果開(kāi)始沒(méi)有內(nèi)容可以設(shè)置為nil,attributes文件的屬性,初始為nil
NSString * fileName = @"testFileNSFileManager.txt";
NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil];
[fileManager createFileAtPath:fileName contents:array attributes:nil];
13、刪除文件
[fileManager removeItemAtPath:fileName error:nil];
相關(guān)文章
iOS擼一個(gè)簡(jiǎn)單路由Router的實(shí)現(xiàn)代碼
這篇文章主要介紹了iOS擼一個(gè)簡(jiǎn)單路由Router的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
iOS版微信朋友圈識(shí)別圖片位置信息 如何實(shí)現(xiàn)?
這篇文章主要為大家詳細(xì)介紹了iOS版微信朋友圈識(shí)別圖片位置信息的實(shí)現(xiàn)方法2016-10-10
iOS?項(xiàng)目嵌入Flutter?運(yùn)行(最新推薦)
這篇文章主要介紹了iOS?項(xiàng)目嵌入Flutter?運(yùn)行,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
教你如何解決XCODE升級(jí)后插件不能用問(wèn)題
Xcode 每次更新有個(gè)很頭疼的問(wèn)題,就是插件都會(huì)失效,要重裝。 不得不說(shuō)好多插件還是非常方便能提高效率。那么如何來(lái)解決這個(gè)問(wèn)題呢,今天我們就來(lái)探討下。2015-11-11
iOS UITextView 首行縮進(jìn) 撤銷輸入 反撤銷輸入的實(shí)現(xiàn)代碼
本文是腳本之家小編給大家分享的iOS UITextView 首行縮進(jìn) 撤銷輸入 反撤銷輸入的實(shí)現(xiàn)代碼,需要的朋友參考下吧2017-09-09
iOS touch事件區(qū)分單擊雙擊響應(yīng)的方法
如果您的 iPhone 應(yīng)用里有個(gè) view,既有單擊操作又有雙擊操作。用戶雙擊 view 時(shí),總是先執(zhí)行一遍單擊的操作再執(zhí)行雙擊的操作。所以直接判斷時(shí)就會(huì)發(fā)現(xiàn)不能直接進(jìn)入雙擊操作。下面是區(qū)分 touch 事件是單擊還是雙擊的方法,需要的朋友可以參考下2016-10-10
IOS 開(kāi)發(fā)之應(yīng)用喚起實(shí)現(xiàn)原理詳解
這篇文章主要介紹了IOS 開(kāi)發(fā)之應(yīng)用喚起實(shí)現(xiàn)原理詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12
iOS runtime動(dòng)態(tài)添加方法示例詳解
Runtime是想要做好iOS開(kāi)發(fā),或者說(shuō)是真正的深刻的掌握OC這門語(yǔ)言所必需理解的東西。下面這篇文章主要給大家介紹了關(guān)于iOS runtime動(dòng)態(tài)添加方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2018-01-01

