查了很多ios 沙盒存儲(chǔ),都是比較老的,不符合現(xiàn)在的情況,看了篇國(guó)外的文章感覺還可以,做下記錄同時(shí)分享給大家,共同討論下;From @lizh荔枝
原文地址:https://swiftlyanand.medium.com/ios-storage-best-practices-294fca83ad9
看圖說明

-
1.Documents 文件夾
?Store only those files in here which should be visible to the user.Contents of this folder is visible in the “Files” application provided it supports “Open in place” and “File sharing” is enabled.Content of the directory is persisted and included in the iCloud and iTunes backups. Disk space used in this directory is reported in the storage settings UI under your app’s “Documents and Data” total.
?Few benefit of exposing the “Document directory” are files become visible in “Files” application and user can find their files in spotlight.
?You can store files at the top level or you can create sub-directories. If you choose to create a sub-directory then keep in mind that not all names are allowed for directory name. I guess few directory names are reserved by Apple as I was not able to create an “Inbox” directory but rest all names that I tried worked.
? 只存儲(chǔ)那些文件在這里,應(yīng)該是可見的用戶。這個(gè)文件夾的內(nèi)容在“文件”應(yīng)用程序中可見,前提是它支持“就地打開”和“文件共享”功能。目錄的內(nèi)容被持久化并包含在iCloud和iTunes備份中。該目錄中使用的磁盤空間在應(yīng)用程序的“文檔和數(shù)據(jù)”總數(shù)下的存儲(chǔ)設(shè)置UI中報(bào)告。
? 公開“文檔目錄”的好處是文件可以在“文件”應(yīng)用程序中看到,用戶可以在聚光燈下找到他們的文件。
?您可以在頂層存儲(chǔ)文件,也可以創(chuàng)建子目錄。如果您選擇創(chuàng)建子目錄,那么請(qǐng)記住,不是所有的名稱都允許作為目錄名。我猜蘋果保留了一些目錄名,因?yàn)槲也荒軇?chuàng)建一個(gè)“收件箱”目錄,但我嘗試過的所有名稱都是有效的。
??????注意 -要啟用“就地打開”,在Info中添加“應(yīng)用程序支持iTunes文件共享”或“UIFileSharingEnabled”鍵,值為“YES”。在Info.plist中添加“LSSupportsOpeningDocumentsInPlace”或“支持就地打開文檔”鍵,值為“YES”,以啟用“文件共享”。

-
2. Library / Application Support 文件夾
數(shù)據(jù)庫(kù)可以放在此文件夾
? Store files in here that are required for your app but should never be visible to the user like your app’s database file.You can store files in here at the top level or create sub-directories.Content of the directory is persisted and included in the iCloud and iTunes backups.You can opt files out if they don’t need to be backed-up.Disk space used in the application support directory is reported in the storage settings UI under your app’s “Documents and Data” total.
? 在這里存儲(chǔ)你的應(yīng)用程序需要的文件,但不應(yīng)該對(duì)用戶可見,比如你的應(yīng)用程序的數(shù)據(jù)庫(kù)文件。您可以在這里的頂層存儲(chǔ)文件或創(chuàng)建子目錄。目錄的內(nèi)容被持久化并包含在iCloud和iTunes備份中。如果文件不需要備份,你可以選擇退出。應(yīng)用程序支持目錄中使用的磁盤空間在應(yīng)用程序的“文檔和數(shù)據(jù)”總數(shù)下的存儲(chǔ)設(shè)置UI中報(bào)告。
??????Note — Unlike other directories mentioned here, “Application support” directory is not present by default in you application’s “Data container” and you need to create it first.
注意:不像這里提到的其他目錄,“應(yīng)用程序支持”目錄在應(yīng)用程序的“數(shù)據(jù)容器”中默認(rèn)不存在,你需要首先創(chuàng)建它。
-
3. Library / Cache
緩存文件
?Stores files in here that can be discarded when the space is low.This is a good location for any content that can be re-downloaded when needed.Contents of this directory is not included in the backups.When the device is low on disk space then iOS can help by clearing caches.Files will never be removed from your cache if your application is running and OS will start by clearing caches from apps that haven’t been used in a while.Files stored on the caches directory is not reported in the storage settings UI under your app’s “Documents and Data” total.
?在這里存儲(chǔ)可以在空間不足時(shí)丟棄的文件。對(duì)于任何可以在需要時(shí)重新下載的內(nèi)容,這都是一個(gè)很好的位置。備份文件中不包含此目錄的內(nèi)容。當(dāng)設(shè)備的磁盤空間不足時(shí),iOS可以幫助清理緩存。如果你的應(yīng)用程序正在運(yùn)行,文件將永遠(yuǎn)不會(huì)從你的緩存中刪除,操作系統(tǒng)將從一段時(shí)間沒有使用過的應(yīng)用程序中清除緩存。存儲(chǔ)在緩存目錄下的文件在你的應(yīng)用程序的“文檔和數(shù)據(jù)”總數(shù)下的存儲(chǔ)設(shè)置UI中沒有報(bào)告。
-
4. tmp —
關(guān)閉APP后,系統(tǒng)會(huì)自動(dòng)刪除
?Put files in here that your won’t be needing for extended period of time.Even though operating system will periodically remove these files when your app is not running, it’s best to remove them when you are done with them.Like the cache directory, tmp directory is not backed up and is not reported in your apps “Documents and Data” total.
?把你以后不需要的文件放在這里。盡管操作系統(tǒng)會(huì)在應(yīng)用程序不運(yùn)行時(shí)定期刪除這些文件,但最好是在使用完它們后刪除它們。和緩存目錄一樣,tmp目錄沒有備份,也沒有在你的應(yīng)用程序“文檔和數(shù)據(jù)”總數(shù)中報(bào)告。
其他附加說明????

/MyApp.app
1.程序包目錄.
2.儲(chǔ)存程序運(yùn)行必須用到的數(shù)據(jù)資源(音頻/視頻/圖片/編譯好的代碼/Nib等).
3.只能讀取(程序安裝時(shí)會(huì)對(duì)程序包進(jìn)行簽名,如程序運(yùn)行時(shí)修改程序包數(shù)據(jù),簽名會(huì)失效,導(dǎo)致程序無法運(yùn)行).
4.iTunes/iCloud同步不會(huì)備份此目錄./Documents
1.用戶文件目錄.
2.儲(chǔ)存程序運(yùn)行中生成的用戶數(shù)據(jù)資源(如從網(wǎng)上下載的圖片/音樂等).
3.可以通過系統(tǒng)自帶的文件App,訪問此路徑資源.
4.iTunes/iCloud同步會(huì)備份此目錄和所有子目錄./Documents/Inbox
1.收件箱目錄.
2.儲(chǔ)存由其他程序請(qǐng)求當(dāng)前程序打開的文件(其他程序中的某個(gè)文件需要用此程序打開,會(huì)把那個(gè)文件復(fù)制一份到此目錄).
3.可讀取,可刪除,不能新建和編輯./Library
1.系統(tǒng)文件目錄.
2.儲(chǔ)存程序運(yùn)行中生成的系統(tǒng)數(shù)據(jù)資源(如文本文件/系統(tǒng)緩存文件等).
3.可創(chuàng)建子文件夾,此目錄中的資源不會(huì)公開給用戶,也不要儲(chǔ)存用戶相關(guān)的數(shù)據(jù).
4.iTunes/iCloud同步會(huì)備份此目錄和所有子目錄(Caches子目錄除外)./Library/Caches
1.系統(tǒng)緩存目錄.
2.儲(chǔ)存程序再次啟動(dòng)時(shí)需要的支持文件/緩存文件和日志文件./Library/Preferences
保存應(yīng)用程序的所有偏好設(shè)置iOS的Settings(設(shè)置),我們不應(yīng)該直接在這里創(chuàng)建文件,而是需要通過NSUserDefault這個(gè)類來訪問應(yīng)用程序的偏好設(shè)置。
iTunes會(huì)自動(dòng)備份該文件目錄下的內(nèi)容。
比如說:是否允許訪問圖片,是否允許訪問地理位置......
1.系統(tǒng)偏好目錄.
2.儲(chǔ)存程序的偏好設(shè)置數(shù)據(jù)資源(Plist文件).
/Library/SplashBoard
1.系統(tǒng)啟動(dòng)圖目錄.
2.儲(chǔ)存程序啟動(dòng)圖緩存數(shù)據(jù)資源.
3.更新啟動(dòng)圖后還是顯示舊啟動(dòng)圖可嘗試清空此目錄數(shù)據(jù)./Library/Application Support
1.系統(tǒng)支持目錄.
2.儲(chǔ)存除用戶相關(guān)的數(shù)據(jù)以外所有的數(shù)據(jù)資源(如游戲新關(guān)卡等).
3.蘋果建議此目錄中所有數(shù)據(jù)放在自定義子目錄中,子目錄名稱以程序Bundle Identifier或公司名命名./Temp
1.臨時(shí)文件目錄.
2.儲(chǔ)存程序本次運(yùn)行時(shí)所需要的臨時(shí)數(shù)據(jù)資源(如調(diào)用系統(tǒng)相機(jī)拍攝的照片/視頻等).
3.iPhone重啟時(shí),會(huì)自動(dòng)清除該目錄下所有文件,所以照片/視頻要及時(shí)保存到系統(tǒng)相冊(cè)中.
4.蘋果建議編程人員應(yīng)該在數(shù)據(jù)長(zhǎng)時(shí)間不使用時(shí)主動(dòng)刪除該目錄下的文件.
4.iTunes/iCloud同步不會(huì)備份此目錄.獲取對(duì)應(yīng)目錄路徑
NSSearchPathForDirectoriesInDomains();
1.查找文件路徑函數(shù)
2.返回值:是一個(gè)數(shù)組,只有一個(gè)NSString元素,即查找的路徑
3.參數(shù)1:NSDocumentDirectory 表示需要查找的目錄
4.參數(shù)2:NSUserDomainMask 表示在用戶的主目錄中查找
5.參數(shù)3:YES 表示返回路徑展開
// 沙盒根目錄
NSString *homePath = NSHomeDirectory();
// Documents目錄
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
// Library目錄
NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
// Caches目錄
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
// Preferences目錄
NSString *preferencesPath = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES).firstObject;
// Application Support目錄
NSString *applicationSupportPath = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).firstObject;
// Temp目錄
NSString *tempPath = NSTemporaryDirectory();
注:此處列出常用的幾種獲取目錄路徑的函數(shù)方法,更多詳細(xì)內(nèi)容可參看[iOS數(shù)據(jù)持久化]系列的NSBundle/NSFileManager等.