iOS--沙盒機制(sandbox)及相關操作簡略瞭解

iOS和Android在資源儲存上是有區別的,在Android平臺,雖然說每一應用都會有自己獨立的空間,這份獨立的空間儲存了應用的信息和數據庫,但是在開發的時候會更多時候把一些下載資源或圖片資源直接放在SD等外部儲存中。但是來到了iOS平臺就不是這樣了,iOS平臺中每一應用獨享一份儲存空間,這份獨享的空間只能允許應用自身訪問,其他應用不能進行訪問,而且應用不能訪問到其他應用的獨享空間,這麼一個空間就叫到沙盒(sandbox)。這只是我自己個人獨立的見解,有所錯誤或不足望能提出,共同學習。

sandbox的目錄結構:

Document:蘋果建議將程序中建立的或在程序中瀏覽到的文件數據保存在該目錄下,iTunes備份和恢復的時候會包括此目錄

Library:儲存應用的默認設置和其他狀態信息

Library/Caches:儲存緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應用退出刪除

tmp:提供一個即時創建臨時文件的地方,重啓時這個文件夾的內容將被清空

進行示例Test中模擬器沙盒文件夾中可看到如下圖文件和文件夾:


在程序中需要使用這幾個文件夾的話,獲取相應文件夾的代碼如下:

    // 獲得應用的Document文件夾位置路徑
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"documentPath:%@", [documentPaths objectAtIndex:0]);
    // 獲得應用的Library文件夾位置路徑
    NSArray *libraryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSLog(@"libraryPath:%@", [libraryPaths objectAtIndex:0]);
    // 獲得應用的Library/Caches文件夾位置路徑
    NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSLog(@"cachePath:%@", [cachesPaths objectAtIndex:0]);
    // 獲得應用的Tmp文件夾位置路徑
    NSString *tmpPath = NSTemporaryDirectory();
    NSLog(@"tmpPath:%@", tmpPath);

把數據寫入文件中,兩個操作,第一個是把一個數組寫入到Document文件夾的data.txt文件中,第二個是把一個字符串寫入到Document文件夾的data1.txt文件中

    // 把數據寫入文件
    NSString *documentPath = [documentPaths objectAtIndex:0];
    // 需要寫入的數據(數組)
    NSArray *dataArray = @[@"hello", @"world", @"你好"];
    // 寫入文件路徑
    NSString *document = [documentPath stringByAppendingPathComponent:@"data.txt"];
    // 寫入
    [dataArray writeToFile:document atomically:YES];
    NSString *document1 = [documentPath stringByAppendingPathComponent:@"data1.txt"];
    // 寫入(字符串)
    [@"helloworld" writeToFile:document1 atomically:YES encoding:NSUTF8StringEncoding error:nil];
運行結果就是可看到Document文件夾中多了兩個文件(data.txt和data1.txt)


打開這兩個文件,可看出,如果是儲存純數組的話,系統會把數組轉換成xml格式的plist文件,而字符串則正常爲字符串。

data.txt:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<string>hello</string>
	<string>world</string>
	<string>你好</string>
</array>
</plist>
data1.txt:

helloworld


從文件中讀取數據,一樣兩個操作,第一個是從文件“data.txt”中讀取數組數據,第二個是從文件“data1.txt”中讀取字符串數據

    // 從文件讀取數據
    NSString *documentPath = [documentPaths objectAtIndex:0];
    // 文件路徑
    NSString *document = [documentPath stringByAppendingPathComponent:@"data.txt"];
    // 文件路徑1
    NSString *document1 = [documentPath stringByAppendingPathComponent:@"data1.txt"];
    // 讀取
    NSArray *dataFromFile = [[NSArray alloc] initWithContentsOfFile:document];
    NSString *dataFromFile1 = [[NSString alloc] initWithContentsOfFile:document1 encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"數組數據:%@", dataFromFile);
    NSLog(@"字符串數據:%@", dataFromFile1);

結果:



至此,結束~~

本文示例Demo放在Github:https://github.com/gaussli/SandboxTest





發佈了32 篇原創文章 · 獲贊 0 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章