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是你本機的用戶名

 


 

然后按下圖進入相應的文件夾,就可以到模擬器的沙盒文件目錄了:

接着進入一個模擬器版本,我這里是5.1

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

分別是.app文件,這個就是可運行的應用文件,Documents,蘋 果建議將程序中創建的或在程序中瀏覽到的文件數據保存在該目錄下,iTunes備份和恢復的時候會包括此目錄;Library,存儲程序的默認設置或其它 狀態信息;Library/Caches:存放緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應用退出刪除;tmp,創建和存放臨時文件的地 方。

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

 1     //獲取根目錄  
 2         NSString *homePath = NSHomeDirectory();  
 3         NSLog(@"Home目錄:%@",homePath);  
 4           
 5         //獲取Documents文件夾目錄,第一個參數是說明獲取Doucments文件夾目錄,第二個參數說明是在當前應用沙盒中獲取,所有應用沙盒目錄組成一個數組結構的數據存放  
 6         NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
 7         NSString *documentsPath = [docPath objectAtIndex:0];  
 8         NSLog(@"Documents目錄:%@",documentsPath);  
 9           
10         //獲取Cache目錄  
11         NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
12         NSString *cachePath = [cacPath objectAtIndex:0];  
13         NSLog(@"Cache目錄:%@",cachePath);  
14           
15         //Library目錄  
16         NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);  
17         NSString *libPath = [libsPath objectAtIndex:0];  
18         NSLog(@"Library目錄:%@",libPath);  
19           
20         //temp目錄  
21         NSString *tempPath = NSTemporaryDirectory();  
22         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/

下面開始向目錄里面創建文件,然后向文件里面寫入內容:

 1     NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
 2         NSString *documentsPath = [docPath objectAtIndex:0];  
 3         //寫入文件  
 4         if (!documentsPath) {  
 5             NSLog(@"目錄未找到");  
 6         }else {  
 7             NSString *filePaht = [documentsPath stringByAppendingPathComponent:@"test.txt"];  
 8             NSArray *array = [NSArray arrayWithObjects:@"Title",@"Contents", nil];  
 9             [array writeToFile:filePaht atomically:YES];  
10         }  

創建成功后打開文件夾目錄,可以看到test.txt文件:

接下來是把該文件中的內容讀出來:

1     //讀取文件  
2     NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
3         NSString *documentsPath = [docPath objectAtIndex:0];  
4         NSString *readPath = [documentsPath stringByAppendingPathComponent:@"test.txt"];  
5         NSArray *fileContent = [[NSArrayalloc] initWithContentsOfFile:readPath];  
6         NSLog(@"文件內容:%@",fileContent);  

輸出結果如下:

2012-08-03 11:26:53.594 SandBoxTest[12642:f803] 文件內容:(

    Title,

    Contents

)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM