iOS沙箱簡單介紹

來源:互聯網
上載者:User

標籤:

先簡單介紹一下什麼是沙箱:你可以簡單理解成為一個目錄,這個目錄的改動不會對作業系統造成任何損失。(這裡也有一點點介紹)

看看蘋果的沙箱目錄:

再附一張蘋果官方的圖

一個iOS app操作都是在自己的沙箱中進行的。

首先:

Deveices,裡面是各種的模擬器裝置。

然後隨便找一個模擬器裝置。裡面的data是裡面的資料。然後Container裡面Data中有一個Application就是該裝置的安裝軟體。

我們可以看到裡面有好多個(就算我們Reset Content and Settings,也會有。因為裡面有裝置內建的軟體,例如通訊錄,地圖等。)。

隨便開啟一個就可以看到裡面某個程式包括三個目錄:

Documents

Library:又包括Caches和Preferences

temp

其中的

Documents只有使用者產生的檔案、其他的資料以及程式程式不能建立的檔案,這裡的資料通過iCloud自動備份。我們錄製的音頻和拍照的圖片都可以放到這裡。

Library:可以重新下載或者重建的資料應該儲存在Library的caches目錄中。這裡用於盛放緩衝。

tmp:是臨時使用的資料。iCloud不會自動備份這些檔案,這些資料使用完後要隨時刪除,避免佔用使用者裝置空間。

- (NSString *)documentDirectory {    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    return [paths objectAtIndex:0];}

通過上面這個方法可以得到:

/Users/zhanggui/Library/Developer/CoreSimulator/Devices/B6134687-CC10-40DE-BD9D-A2E5AD2D5C03/data/Containers/Data/Application/34361283-8619-481D-B8CB-B25EB4B787E2/Documents

也就是Documents的路徑.

還可以得到通過下面的方法:

 NSString *s = NSHomeDirectory();

得到如下結果:

/Users/zhanggui/Library/Developer/CoreSimulator/Devices/B6134687-CC10-40DE-BD9D-A2E5AD2D5C03/data/Containers/Data/Application/08A6D627-1363-48B4-8AF8-A1ED361DC9D6

故名思議,我們通過NSHomeDirectory得到了該安裝軟體的目錄。(裡面就包括Documents/Library/temp)

蘋果官方介紹的是返回使用者的或者應用程式的家目錄,依賴於平台。

 

再來看一下如何取得Caches目錄路徑的方法:

  NSString *s = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

返回結果:

/Users/zhanggui/Library/Developer/CoreSimulator/Devices/B6134687-CC10-40DE-BD9D-A2E5AD2D5C03/data/Containers/Data/Application/DB647E93-5F05-4B7B-A8E9-F6F01DD2586B/Library/Caches

最後看一下如何擷取tmp目錄:

  NSString *s = NSTemporaryDirectory();

返回結果:

/Users/zhanggui/Library/Developer/CoreSimulator/Devices/B6134687-CC10-40DE-BD9D-A2E5AD2D5C03/data/Containers/Data/Application/C7CA833E-8D20-480B-AB91-234A8F17336D/tmp/

--------------------------------------------------------------------大概就這麼多吧--------------------------------------------------------------------

NO ,NO ,NO看看蘋果官方的說明吧:

AppName.app

This is the app’s bundle. This directory contains the app and all of its resources.

You cannot write to this directory. To prevent tampering, the bundle directory is signed at installation time. Writing to this directory changes the signature and prevents your app from launching. You can, however, gain read-only access to any resources stored in the apps bundle. For more information, see theResource Programming Guide

The contents of this directory are not backed up by iTunes. However, iTunes does perform an initial sync of any apps purchased from the App Store.

Documents/

Use this directory to store user-generated content. The contents of this directory can be made available to the user through file sharing; therefore, his directory should only contain files that you may wish to expose to the user.

The contents of this directory are backed up by iTunes.

Documents/Inbox

Use this directory to access files that your app was asked to open by outside entities. Specifically, the Mail program places email attachments associated with your app in this directory. Document interaction controllers may also place files in it.

Your app can read and delete files in this directory but cannot create new files or write to existing files. If the user tries to edit a file in this directory, your app must silently move it out of the directory before making any changes.

The contents of this directory are backed up by iTunes.

Library/

This is the top-level directory for any files that are not user data files. You typically put files in one of several standard subdirectories. iOS apps commonly use the Application Support and Caches subdirectories; however, you can create custom subdirectories.

Use the Library subdirectories for any files you don’t want exposed to the user. Your app should not use these directories for user data files.

The contents of the Library directory (with the exception of the Caches subdirectory) are backed up by iTunes.

For additional information about the Library directory and its commonly used subdirectories, see The Library Directory Stores App-Specific Files.

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 they are no longer needed; however, the system may purge this directory when your app is not running.

The contents of this directory are not backed up by iTunes.

英語好的大神可以看一下。不好的飄過,,,,

更多請見:https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

 

iOS沙箱簡單介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.