一、各目录详解
1.Documents:只有用户生成的文件、应用程序不能重新创建的文件,应该保存在<Application_Home>/Documents 目录下面,并将通过iCloud自动备份。
2.Library:可以重新下载或者重新生成的数据应该保存在<Application_Home>/Library/Caches 目录下面。举个例子,比如杂志、新闻、地图应用使用的数据库缓存文件和可下载内容应该保存到这个文件夹。
3.tmp:只是临时使用的数据应该保存到<Application_Home>/tmp 文件夹。尽管 iCloud 不会备份这些文件,但在应用在使用完这些数据之后要注意随时删除,避免占用用户设备的空间
关于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,需要和服务器配合,经常从服务器下载东西,展示给用户看。那么这些下载下来的东西就放在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];