您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

詳解 iOS 沙盒機制與文件操作

編輯:iOS 開發綜合

一、什麼是沙盒機制

iOS 應用只能在自己所屬的文件系統區域內讀寫文件,無法越界訪問其他應用的數據,這塊受限的私有空間就被稱為「沙盒(Sandbox)」。因此,所有非代碼資源——圖片、圖標、聲音、視頻、屬性列表、文本文件等——都必須保存在沙盒之內。

  • 每個應用都擁有自己獨立的存儲空間;
  • 應用無法翻越「圍牆」去訪問其他存儲空間的內容。

二、如何打開模擬器沙盒目錄

方法一:顯示隱藏文件後在 Finder 中打開。開啟終端輸入以下命令即可顯示 Mac 隱藏文件:

# 顯示隱藏文件
defaults write com.apple.finder AppleShowAllFiles -bool true
# 隱藏文件
defaults write com.apple.finder AppleShowAllFiles -bool false

執行後即可看到「資源庫」文件夾,接著找到 /Application Support/iPhone Simulator/ 目錄,這裡就是模擬器中各個應用的沙盒所在。

方法二(更方便):在 Finder 菜單中點擊「前往」,此時按住 option 鍵,就會出現「資源庫」選項,後續操作相同。

三、目錄結構

默認情況下,每個沙盒包含三個文件夾:Documents、Library 與 tmp。基於沙盒機制,應用只能在這幾個目錄下讀寫文件:

  • Documents:建議存放程序中創建或瀏覽到的文件數據,iTunes 備份與恢復時會包含此目錄。
  • Library:存儲應用的默認設置或其他狀態信息;其中 Library/Caches 存放緩存文件,iTunes 不會備份,應用退出時也不會刪除。
  • tmp:提供一個可即時創建臨時文件的地方,設備重啟時會丟棄其中的所有文件。

概括來說:iTunes 同步時會備份所有 Documents 與 Library 文件;而 iPhone 重啟時會丟棄全部 tmp 文件。

四、常用文件操作代碼示例

1. 獲取各類目錄路徑

// 程序的 Home 目錄
NSString *homeDirectory = NSHomeDirectory();

// Documents 目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths objectAtIndex:0];

// Caches 目錄
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

// tmp 目錄
NSString *tmpDir = NSTemporaryDirectory();

2. 寫入與讀取文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];

// 寫入
NSArray *array = [[NSArray alloc] initWithObjects:@"內容", @"content", nil];
[array writeToFile:filePath atomically:YES];

// 讀取
NSArray *readArray = [[NSArray alloc] initWithContentsOfFile:filePath];
NSLog(@"%@", readArray);

3. 判斷文件是否存在

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documents stringByAppendingPathComponent:@"test"];

if ([fileManager fileExistsAtPath:filePath]) {
    NSLog(@"文件存在");
}

4. 創建目錄與文件

NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
// 創建目錄
[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];

// 在目錄下創建文件
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];
NSString *string = @"寫入內容,write String";
[fileManager createFileAtPath:testPath contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

5. 獲取目錄下所有文件名

NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
// 方法一
NSArray *file = [fileManage subpathsOfDirectoryAtPath:myDirectory error:nil];
// 方法二
NSArray *files = [fileManage subpathsAtPath:myDirectory];

6. 操作當前目錄並創建文件

NSFileManager *fileManager = [NSFileManager defaultManager];
// 切換到待操作的目錄
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];

NSString *fileName = @"testFileNSFileManager.txt";
NSArray *array = [[NSArray alloc] initWithObjects:@"hello world", @"hello world1", @"hello world2", nil];
// contents 為文件內容(可為 nil),attributes 為文件屬性(初始為 nil)
[fileManager createFileAtPath:fileName contents:array attributes:nil];

7. 刪除文件

[fileManager removeItemAtPath:fileName error:nil];
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved