【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文件。

下面是獲取文件目錄的代碼以及讀寫文件的代碼:示例Demo免費下載地址:http://download.csdn.net/detail/weasleyqi/7508141

獲取根目錄:

    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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章