舉例詳解iOS開發(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)容
打開模擬器沙盒目錄
方法1、可以設(shè)置顯示隱藏文件,然后在Finder下直接打開。設(shè)置查看隱藏文件的方法如下:打開終端,輸入命名
<p class="p1">顯示Mac隱藏文件的命令:
隱藏Mac隱藏文件的命令:
現(xiàn)在能看到資源庫(kù)文件夾了。

打開資源庫(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)容,如果開始沒(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在Block中修改外部變量值的實(shí)現(xiàn)代碼
這篇文章主要介紹了iOS在Block中修改外部變量值的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08iOS 中使用tableView實(shí)現(xiàn)右滑顯示選擇功能
這篇文章主要介紹了iOS 中使用tableView實(shí)現(xiàn)右滑顯示選擇功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07iOS中使用NSProgress類來(lái)創(chuàng)建UI進(jìn)度條的方法詳解
NSProgress是iOS7以后引入的用于制作進(jìn)度條的類,能夠監(jiān)聽多個(gè)任務(wù),這里就為大家?guī)?lái)iOS中使用NSProgress類來(lái)創(chuàng)建UI進(jìn)度條的方法詳解,需要的朋友可以參考下2016-06-06iOS對(duì)象指針和基礎(chǔ)數(shù)據(jù)類型的強(qiáng)轉(zhuǎn)詳解
最近在做一些小功能,忽然發(fā)現(xiàn)有的基礎(chǔ)數(shù)據(jù)轉(zhuǎn)換居然都忘記了。于是想著要趕緊整理下記下來(lái)!本文就是記錄的一些內(nèi)容,主要介紹了iOS中對(duì)象指針和基礎(chǔ)數(shù)據(jù)類型的強(qiáng)轉(zhuǎn),有需要的朋友們可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11WKWebview非全屏自動(dòng)播放h5視頻的實(shí)現(xiàn)方法(Swift、OC)
這篇文章主要給大家介紹了關(guān)于WKWebview非全屏自動(dòng)播放h5視頻的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05iOS長(zhǎng)按UIlabel實(shí)現(xiàn)可復(fù)制功能
在我們?nèi)粘5拈_發(fā)中經(jīng)常會(huì)遇到一些小需求,比如需要長(zhǎng)按控件來(lái)拷貝控件中得內(nèi)容,所以這篇文章跟大家分享下iOS中長(zhǎng)按UIlabel實(shí)現(xiàn)可復(fù)制功能的方法,有需要的朋友們可以參考借鑒。2016-09-09利用iOS實(shí)現(xiàn)系統(tǒng)相冊(cè)大圖瀏覽功能詳解
查看大圖是們?nèi)粘i_發(fā)中經(jīng)常會(huì)遇到的一個(gè)需求,下面這篇文章主要給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)系統(tǒng)相冊(cè)大圖瀏覽功能的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-09-09iOS中UIScrollerView的用法及基于AotoLayout的控件懸停
這篇文章主要介紹了iOS中UIScrollerView的用法及基于AotoLayout的控件懸停,文中對(duì)于UIScrollerView的方法及屬性介紹地非常詳細(xì),十分推薦,示例代碼為Objective-C,需要的朋友可以參考下2016-03-03