一锻拘、各目錄詳解
1.Documents:只有用戶生成的文件油吭、應(yīng)用程序不能重新創(chuàng)建的文件,應(yīng)該保存在<Application_Home>/Documents 目錄下面署拟,并將通過iCloud自動備份婉宰。
2.Library:可以重新下載或者重新生成的數(shù)據(jù)應(yīng)該保存在<Application_Home>/Library/Caches 目錄下面。舉個例子推穷,比如雜志心包、新聞、地圖應(yīng)用使用的數(shù)據(jù)庫緩存文件和可下載內(nèi)容應(yīng)該保存到這個文件夾馒铃。
3.tmp:只是臨時使用的數(shù)據(jù)應(yīng)該保存到<Application_Home>/tmp 文件夾蟹腾。盡管 iCloud 不會備份這些文件,但在應(yīng)用在使用完這些數(shù)據(jù)之后要注意隨時刪除区宇,避免占用用戶設(shè)備的空間
關(guān)于iOS Data Storage Guidelines 岭佳,請參考:iOS Data Storage Guidelines - Apple Developer
二、 以下是幾個要點的摘錄:
To ensure that backups are as efficient as possible, store your app’s data according to the following guidelines:
1萧锉、Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the/Documents directory and will be automatically backed up by iCloud.
2、Data that can be downloaded again or regenerated should be stored in the/Library/Caches directory. Examples of files you should put in the Caches directory include database cache files and downloadable content, such as that used by magazine, newspaper, and map applications.
3述寡、Data that is used only temporarily should be stored in the/tmp directory. Although these files are not backed up to iCloud, remember to delete those files when you are done with them so that they do not continue to consume space on the user’s device.
三柿隙、最通俗的理解方式:
如果你做個記事本的app,那么用戶寫了東西鲫凶,總要把東西存起來禀崖。那么這個文件則是用戶自行生成的,就放在documents文件夾里面螟炫。
如果你有一個app波附,需要和服務(wù)器配合,經(jīng)常從服務(wù)器下載東西昼钻,展示給用戶看掸屡。那么這些下載下來的東西就放在library/cache。
apple對這個很嚴格然评,放錯了就會被拒仅财。主要原因是ios的icloud的同步問題。
四碗淌、最后附上獲取App文件目錄的方法:
//Home目錄
NSString *homeDirectory = NSHomeDirectory();
//Document目錄 documents (Documents)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = [paths objectAtIndex:0];
//Libaray目錄 various documentation, support, and configuration files, resources (Library)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
NSString *path = [paths objectAtIndex:0];
//Cache目錄 location of discardable cache files (Library/Caches)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
NSString *path = [paths objectAtIndex:0];