iOS 沙盒機制及檔案操作
沙盒機制簡介
iOS應用程式只能在為該程式建立的檔案目錄下進行檔案的讀寫操作,不可以去其它地方訪問,此檔案區域被成為沙盒。
每個iOS應用都有自己的應用沙盒,應用沙盒就是檔案系統目錄,沙盒對應用程式執行各種操作的規定了區域範圍的許可權限制。
特點:
1.每個應用程式的活動範圍都限定在自己的沙盒裡
2.不能隨意跨越自己的沙盒去訪問別的應用程式沙盒中的內容
(iOS8已經部分開放訪問extension)
3.在訪問別人沙盒內的資料時需要訪問許可權。
概括:
1.是一種安全體系的表現
2.總體來說沙盒就是一種獨立、安全、封閉的空間。
3.非程式碼檔案都要儲存在此,例如影象,圖示,聲音,映像,屬性列表,文字檔案等。
開啟沙盒目錄
模擬器沙盒
方法1、可以設定顯示隱藏檔案,然後在Finder下直接開啟。設定檢視隱藏檔案的方法如下:開啟終端,輸入命令
顯示Mac隱藏檔案的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
隱藏Mac隱藏檔案的命令:defaults write com.apple.finder AppleShowAllFiles -bool false
輸完單擊Enter鍵,退出終端,重新啟動Finder就可以了重啟Finder:滑鼠單擊視窗左上角的蘋果標誌-->強制退出-->Finder-->
方法2、這種方法更方便,在Finder上點->前往->前往資料夾,輸入/Users/使用者名稱/Library/Developer/CoreSimulator/Devices/EC823725-0694-4879-ADBE-423B7C3DF01F/data/Containers/Data/Application 前往。
沙盒內部檔案作用:
Documents:儲存應用執行時生成的需要持久化的資料,iTunes會自動備份該目錄。
蘋果建議將在應用程式中瀏覽到的檔案資料儲存在該目錄下。
Library:
Caches:
一般儲存的是快取檔案,例如圖片視訊等,此目錄下的檔案不會再應用程式退出時刪除。
*在手機備份的時候,iTunes不會備份該目錄。
例如音訊,視訊等檔案存放其中
Preferences:
儲存應用程式的所有偏好設定iOS的Settings(設定),我們不應該直接在這裡建立檔案,
而是需要通過NSUserDefault這個類來訪問應用程式的偏好設定。
*iTunes會自動備份該檔案目錄下的內容。
比如說:是否允許訪問圖片,是否允許訪問地理位置......
tmp:臨時檔案目錄,在程式重新執行的時候,和開機的時候,會清空tmp資料夾。
手機沙盒 目錄路徑: /var/mobile/Applications
通過程式碼開啟沙盒
1).獲取沙盒的Home目錄
NSString *homePath = NSHomeDirectory();
2).獲取沙盒的Documents目錄
@param NSDocumentDirectory 獲取Document目錄
@param NSUserDomainMask 是在當前沙盒範圍內查詢
@param YES 展開路徑,NO是不展開
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
3).獲取Library檔案路徑
@param NSLibraryDirectory 獲取Library目錄
@param NSUserDomainMask 在當前的沙盒範圍內查詢
@param YES 展開路徑,NO不展開路徑 *
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
4).獲取Library/Caches檔案目錄
@param NSCachesDirectory 獲取Library/Caches目錄
@param NSUserDomainMask 在當前的沙盒範圍內查詢
@param YES 展開路徑,NO不展開路徑 *
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
5).獲取Library/Preferences檔案目錄
Preferences由系統維護,不需要我們手動的獲取檔案路徑進行操作,而是需要藉助NSUserDefault來操作,但是我們是可以獲取到這個檔案的。
@param NSLibraryDirectory 獲取Library目錄
@param NSUserDomainMask 在當前的沙盒範圍內查詢
@param YES 展開路徑,NO不展開路徑 *
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"Preferences"];
6).獲取tmp檔案路徑
NSString *filePath = NSTemporaryDirectory();
向沙盒中讀寫檔案
寫入檔案
NSArray *array = [NSArray arrayWithObjects:@"write",@"to", @“sandbox”, nil];
[array writeToFile:filePah atomically:YES];
讀取檔案
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePah];
iOS NSFileManager 的使用
1.建立目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *newDirectory = [documentsDirectory stringByAppendingPathComponent:@“newDirectory”];
[fileManager createDirectoryAtPath:newDirectory withIntermediateDirectories:YES attributes:nil error:nil];
2.建立檔案
//用全路徑建立檔案
NSString *myStr = @"寫入內容,write String";
NSString *filePath = [newDirectory stringByAppendingPathComponent:@“newFIle.txt"];
[FileManager createFileAtPath:filePath contents:[myStr dataUsingEncoding:NSUTF8StringEncoding]
attributes:nil];
//更改到待操作的目錄下,再建立檔案
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
NSString * fileName = @"newFIle.txt";
NSArray *array = [[NSArray alloc] initWithObjects:@"hello world”,nil];
[fileManager createFileAtPath:fileName contents:array attributes:nil];
3.獲取指定檔案下所有目錄及檔案(兩個都行)
NSArray *files = [fileManage subpathsOfDirectoryAtPath: newDirectory error:nil];
NSArray *files = [fileManage subpathsAtPath: newDirectory ];
4.刪除檔案
[fileManager removeItemAtPath:@“檔案路徑” error:nil];
5.寫資料
//待寫入的資料
NSString *strTemp = @“hello world”;
int intData = 1992;
float floatData = 3.4f;
//建立資料緩衝
NSMutableData *writer = [[NSMutableData alloc] init];
//將字串新增到緩衝中
[writer appendData:[strTemp dataUsingEncoding:NSUTF8StringEncoding]];
//將其他資料新增到緩衝中
[writer appendBytes:&intData length:sizeof(intData)];
[writer appendBytes:&floatData length:sizeof(floatData)];
//將緩衝的資料寫入到檔案中
[writer writeToFile:filePath atomically:YES];
6.讀資料
int intData;
float floatData = 0.0;
NSString *strData;
NSData *reader = [NSData dataWithContentsOfFile:filePath];
strData = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [strTemp length])]
encoding:NSUTF8StringEncoding];
[reader getBytes:&intData range:NSMakeRange([strTemp length], sizeof(intData))];
[reader getBytes:&floatData range:NSMakeRange([strTemp length] + sizeof(intData), sizeof(floatData))];