IOS沙箱(SandBox)

來源:互聯網
上載者:User

標籤:

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

)

IOS沙箱(SandBox)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.