先簡單介紹一下什么是沙盒:你可以簡單理解成為一個目錄,這個目錄的改動不會對操作系統造成任何損失。(這里也有一點點介紹)
看看蘋果的沙盒目錄:


再附一張蘋果官方的圖

一個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 | 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. |
| 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. |
| 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. |
| 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 Use the The contents of the For additional information about the Library directory and its commonly used subdirectories, see The Library Directory Stores App-Specific Files. |
| 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. |
英語好的大神可以看一下。不好的飄過,,,,
新聞熱點
疑難解答