在我記憶中,沙盒文件夾應該是有四個,可是我今天在模擬器中打開的時候只有三個:Documents、Library、tmp。以下圖:app
上網查了下,還有個AppName.app,接下來一一對這些文件夾作出介紹。spa
一、Documents:數據持久化能夠寫到這個文件夾下,包括用戶信息,或者離線閱讀功能,也就是下載的文件
orm
二、Library:這個文件夾中有兩個子文件夾:Caches、Preferences,與Documents相似,也能夠作數據持久化,可是與Document不一樣的是,Caches文件不能保證完美持久化,根據官方文檔說:在一些特殊狀況下(系統恢復或者磁盤空間過低)會被刪除,所以每次使用裏面的數據時得先查文件是否還存在。遊戲
該圖是Library文件夾的結構,當前被選中的plist文件就是代碼中經常使用到的NSUserDefault類所存儲的文件。ci
三、tmp:這個文件不能存儲持久化數據,每次程序再次打開的時候會被清空。
資源
四、AppName.app:看名字就應該知道了,就是應用程序的安裝包,包含應用程序自己,不能對該文件作修改。文檔
注:以上四個文件夾都存放在一個名爲Application_Home的文件夾中。it
如下是每一個文件夾的獲取方法:
io
一、Application_Home軟件
NSString *homeDir = NSHomeDirectory();
二、Documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
三、Caches
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
四、tmp
NSString *tmpDir = NSTemporaryDirectory();
五、獲取資源文件的方法,以icon.png爲例:
[[NSBundle mainBundle] pathForResource:@」icon」 ofType:@」png」];
關於Documents和Library文件夾的官方敘述以下(軟件升級過程):
Files Saved During Application Updates When a user downloads an application update, iTunes installs the update in a new application directory. It then moves the user’s data files from the old installation over to the new application directory before deleting the old installation. Files in the following directories are guaranteed to be preserved during the update process:
Application_Home/Documents
Application_Home/Library
Although files in other user directories may also be moved over, you should not rely on them being present after an update.
說簡單就是在升級的時候,系統先建立一個新的應用目錄,在該目錄下安裝新最新軟件,再把舊目錄下的數據拷貝過來,以後再刪除舊目錄。此時應該注意:之前作遊戲的時候用戶關卡數據須要先讀取,而後再寫文件,具體過程如今有點記不清了。