1. 程式人生 > >【iOS知識學習】_iOS沙盒機制

【iOS知識學習】_iOS沙盒機制

IOS中的沙盒機制(SandBox)是一種安全體系,它規定了應用程式只能在為該應用建立的資料夾內讀取檔案,不可以訪問其他地方的內容。所有的非程式碼檔案都儲存在這個地方,比如圖片、聲音、屬性列表和文字檔案等。
1.每個應用程式都在自己的沙盒內
2.不能隨意跨越自己的沙盒去訪問別的應用程式沙盒的內容
3.應用程式向外請求或接收資料都需要經過許可權認證

顯示和隱藏資料夾的方法:
顯示Mac隱藏檔案的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
隱藏Mac隱藏檔案的命令:defaults write com.apple.finder AppleShowAllFiles -bool false

然後重新啟動Finder,點選螢幕左上角蘋果標誌——強制退出——選擇Finder然後點選重新啟動,這個時候在重新開啟Finder就可以看到被隱藏的檔案了。

模擬器裡的內容路徑如下:




每個應用程式都有自己獨有的目錄:
/var/mobile/Applications/UUID
當應用程式安裝時,UUID隨機產生;

當刪除應用程式重新安裝的時候會重新生成目錄

每個app目錄下都有以下幾個資料夾:

Documents/

Library/

Library/Caches

Library/Preferences

tmp/

.app


每個檔案(資料夾)的功能如下:

.app/,這個就是可執行的應用檔案,帶有簽名的檔案包,包含應用程式程式碼和靜態資料;
Documents/
:Use this directory to store critical user documents and app data files. Critical data is any data that cannot be recreated by your app, such as user-generated content.
The contents of this directory can be made available to the user through file sharing. The contents of this directory are backed up by iTunes.
:蘋果建議將程式中建立的或在程式中瀏覽到的檔案資料儲存在該目錄下,iTunes備份和恢復的時候會包括此目錄;
Library/


:This directory is the top-level directory for files that are not user data files. You typically put files in one of several standard subdirectories but you can also create custom subdirectories for files you want backed up but not exposed to the user. You should not use this directory for user data files.
The contents of this directory (with the exception of the Caches subdirectory) are backed up by iTunes.
For additional information about the Library directory, see “The Library Directory Stores App-Specific Files.”
:應用程式支援檔案;
Library/Preferences/
:儲存程式的預設設定或其它狀態資訊;
Library/Caches/:存放快取檔案,iTunes不會備份此目錄,此目錄下檔案不會在應用退出刪除;當系統清理磁碟空間的時候會刪除裡面的內容。
tmp/
:Use this directory to write temporary files that do not need to persist between launches of your app. Your app should remove files from this directory when it determines they are no longer needed. (The system may also purge lingering files from this directory when your app is not running.)
In iOS 2.1 and later, the contents of this directory are not backed up by iTunes.
:建立和存放臨時檔案的地方,應用程式成功啟動,臨時檔案不需要持久儲存。

注:

iTunes在與iPhone同步時,備份所有的Documents和Library檔案。
iPhone在重啟時,會丟棄所有的tmp檔案。

獲取根目錄:

    NSString *homePath = NSHomeDirectory();
    NSLog(@"Home目錄:%@",homePath);
獲取Document目錄:
    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [docPath objectAtIndex:0];

獲取Library目錄:
    NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libPath = [libsPath objectAtIndex:0];

獲取Cache目錄:
    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [cacPath objectAtIndex:0];

獲取Tmp目錄:
    NSString *tempPath = NSTemporaryDirectory();
    NSLog(@"temp目錄:%@",tempPath);

寫入檔案:
    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [docPath objectAtIndex:0];
    //寫入檔案
    if (!documentsPath) {
        NSLog(@"目錄未找到");
    }else {
        NSString *filePaht = [documentsPath stringByAppendingPathComponent:@"test.txt"];
        NSArray *array = [NSArray arrayWithObjects:@"Title",@"Contents", nil];
        [array writeToFile:filePaht atomically:YES];
    }

讀取檔案:
    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [docPath objectAtIndex:0];
    NSString *readPath = [documentsPath stringByAppendingPathComponent:@"test.txt"];
    NSArray *fileContent = [[NSArray alloc] initWithContentsOfFile:readPath];
    NSLog(@"檔案內容:%@",fileContent);



示例Demo下載地址:http://download.csdn.net/detail/weasleyqi/7508141