天天看點

IOS學習—沙盒機制(SandBox)

IOS中的沙盒機制(SandBox)是一種安全體系,它規定了應用程式隻能在為該應用建立的檔案夾内讀取檔案,不可以通路其他地方的内容。所有的非代碼檔案都儲存在這個地方,比如圖檔、聲音、屬性清單和文本檔案等。

1.每個應用程式都在自己的沙盒内

2.不能随意跨越自己的沙盒去通路别的應用程式沙盒的内容

3.應用程式向外請求或接收資料都需要經過權限認證

檢視模拟器的沙盒檔案夾在Mac電腦上的存儲位置,首先,這個檔案夾是被隐藏的,是以要先将這些檔案顯示出來,打開指令行:

顯示Mac隐藏檔案的指令:defaults write com.apple.finder AppleShowAllFiles -bool true

隐藏Mac隐藏檔案的指令:defaults write com.apple.finder AppleShowAllFiles -bool false

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

還有一種比較簡單的辦法就是直接點選Finder圖示右鍵——前往檔案夾——輸入/Users/your username/Library/Application Support/iPhone Simulator/ ,然後确認就可以了。your username是你本機的使用者名

然後按下圖進入相應的檔案夾,就可以到模拟器的沙盒檔案目錄了:

IOS學習—沙盒機制(SandBox)

接着進入一個模拟器版本,我這裡是5.1

IOS學習—沙盒機制(SandBox)

然後可以看到Applications下面存放的就是模拟器中所裝的開發的應用程式,随便進入一個後可以看到,一個沙盒中包含了四個部分,如圖所示:

IOS學習—沙盒機制(SandBox)

分别是.app檔案,這個就是可運作的應用檔案,Documents,蘋果建議将程式中建立的或在程式中浏覽到的檔案資料儲存在該目錄下,iTunes備份和恢複的時候會包括此目錄;Library,存儲程式的預設設定或其它狀态資訊;Library/Caches:存放緩存檔案,iTunes不會備份此目錄,此目錄下檔案不會在應用退出删除;tmp,建立和存放臨時檔案的地方。

下面通過代碼來擷取這些目錄:

[cpp]  view plain copy print ?

//擷取根目錄   

    NSString *homePath = NSHomeDirectory();  

    NSLog(@"Home目錄:%@",homePath);  

    //擷取Documents檔案夾目錄,第一個參數是說明擷取Doucments檔案夾目錄,第二個參數說明是在目前應用沙盒中擷取,所有應用沙盒目錄組成一個數組結構的資料存放   

    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  

    NSString *documentsPath = [docPath objectAtIndex:0];  

    NSLog(@"Documents目錄:%@",documentsPath);  

    //擷取Cache目錄   

    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  

    NSString *cachePath = [cacPath objectAtIndex:0];  

    NSLog(@"Cache目錄:%@",cachePath);  

    //Library目錄   

    NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);  

    NSString *libPath = [libsPath objectAtIndex:0];  

    NSLog(@"Library目錄:%@",libPath);  

    //temp目錄   

    NSString *tempPath = NSTemporaryDirectory();  

    NSLog(@"temp目錄:%@",tempPath);  

//擷取根目錄      NSString *homePath = NSHomeDirectory();      NSLog(@"Home目錄:%@",homePath);            //擷取Documents檔案夾目錄,第一個參數是說明擷取Doucments檔案夾目錄,第二個參數說明是在目前應用沙盒中擷取,所有應用沙盒目錄組成一個數組結構的資料存放      NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);      NSString *documentsPath = [docPath objectAtIndex:0];      NSLog(@"Documents目錄:%@",documentsPath);            //擷取Cache目錄      NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);      NSString *cachePath = [cacPath objectAtIndex:0];      NSLog(@"Cache目錄:%@",cachePath);            //Library目錄      NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);      NSString *libPath = [libsPath objectAtIndex:0];      NSLog(@"Library目錄:%@",libPath);            //temp目錄      NSString *tempPath = NSTemporaryDirectory();      NSLog(@"temp目錄:%@",tempPath);        

輸出結果如下:

2012-08-03 11:10:24.325 SandBoxTest[12549:f803] Home目錄:/Users/Ryan/Library/Application Support/iPhone Simulator/5.1/Applications/A6B99E5A-E2C7-46E9-867A-4E7619F0DA45

2012-08-03 11:10:24.325 SandBoxTest[12549:f803] Documents目錄:/Users/Ryan/Library/Application Support/iPhone Simulator/5.1/Applications/A6B99E5A-E2C7-46E9-867A-4E7619F0DA45/Documents

2012-08-03 11:10:24.326 SandBoxTest[12549:f803] Cache目錄:/Users/Ryan/Library/Application Support/iPhone Simulator/5.1/Applications/A6B99E5A-E2C7-46E9-867A-4E7619F0DA45/Library/Caches

2012-08-03 11:10:24.326 SandBoxTest[12549:f803] Library目錄:/Users/Ryan/Library/Application Support/iPhone Simulator/5.1/Applications/A6B99E5A-E2C7-46E9-867A-4E7619F0DA45/Library

2012-08-03 11:10:24.326 SandBoxTest[12549:f803] temp目錄:/var/folders/7z/1wj5h8zx7b59c02pxmpynd500000gn/T/

下面開始向目錄裡面建立檔案,然後向檔案裡面寫入内容:

[cpp]  view plain copy print ?

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];      //寫入檔案      if (!documentsPath) {          NSLog(@"目錄未找到");      }else {          NSString *filePaht = [documentsPath stringByAppendingPathComponent:@"test.txt"];          NSArray *array = [NSArray arrayWithObjects:@"Title",@"Contents", nil];          [array writeToFile:filePaht atomically:YES];      }        

[cpp]  view plain copy print ?

建立成功後打開檔案夾目錄,可以看到test.txt檔案:

IOS學習—沙盒機制(SandBox)

接下來是把該檔案中的内容讀出來:

[cpp]  view plain copy print ?

//讀取檔案   

NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  

    NSString *documentsPath = [docPath objectAtIndex:0];  

    NSString *readPath = [documentsPath stringByAppendingPathComponent:@"test.txt"];  

    NSArray *fileContent = [[NSArrayalloc] initWithContentsOfFile:readPath];  

    NSLog(@"檔案内容:%@",fileContent);  

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

輸出結果如下:

2012-08-03 11:26:53.594 SandBoxTest[12642:f803] 檔案内容:(

    Title,

    Contents

)

原文位址:http://wuyunhe.5945.blog.163.com/blog/static/58979048201302381430331/