0%

iOS app文件系统介绍

iOS app文件系统

app沙盒:

AppName.app

app的bundle。该目录包含APP程序及开发过程中用到的本地资源。该目录是只读的,不可修改否则APP将无法启动。

Documents目录

说明:

1
2
3
Use this directory to store user-generated content. The contents of this directory can be made available to the user through file sharing; therefore, this directory should only contain files that you may wish to expose to the user.

The contents of this directory are backed up by iTunes and iCloud.

Documents目录主要放置用户创建的相关内容,比如用户的动态,消息数据库等。Documents目录应该仅存放你想暴露给用户的文件。该目录下的文件都会被备份到iTunes和iCloud。

Library目录

该目录下有几个系统创建的子目录,用的比较多的是/Caches和/Preferences.

1
2
3
4
/Library
/Application Support
/Caches
/Preferences

说明:

1
2
3
4
5
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 Application Support and Caches subdirectories; however, you can create custom subdirectories.

Use the Library subdirectories for any files you don’t want exposed to the user. Your app should not use these directories for user data files.

The contents of the Library directory (with the exception of the Caches subdirectory) are backed up by iTunes and iCloud.

Library目录下面有三个系统创建的子目录分别是Application Support,Caches,Preferences,当然你也可以创建自己的子目录。Library目录下通常放一些不想暴露给用户的文件,比如日志文件。

Library目录下除了Caches文件夹,其他的文件都会被备份到iTunes和iCloud。

Caches目录

In iOS 5.0 and later, the system may delete the Caches directory on rare occasions when the system is very low on disk space. This will never occur while an app is running. However, be aware that restoring from backup is not necessarily the only condition under which the Caches directory can be erased.

在手机磁盘不足时,系统会周期性的将不在运行状态APP的Caches目录下的所有文件删除.已经亲身经历过是真的,所以不要将重要数据保存在该目录下.否则出bug时你绝对会想不到是这个原因,而且这样的bug复现难度很大.网上有一些文章中说”系统不会清理 cache 目录中的文件”实属误人子弟,也不知道他们是从哪看到的结论.

由于Caches目录下的文件可能会被系统主动删除,所以Caches目录只适合放一些可重建的,可再次下载的文件,比如图片缓存等。千万不要放不可重建的重要数据,比如存放token信息等,否则出了bug,会很难排查。

Preferences目录

NSUserDefaults保存的东西就在/Preferences目录.

tmp目录

说明:

1
2
3
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 or iCloud.

由于系统在APP不在运行时会清除该目录下的内容。因此tmp目录只适合存放一些临时文件,比如下载过程中产生的临时文件。这些临时文件不再需要时应该及时删除。

tmp目录下的文件不会被备份到iTunes和iCloud。

获取沙盒路径

NSHomeDirectory()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
NSString *homePath = NSHomeDirectory();
NSString *documentsPath = [homePath stringByAppendingPathComponent:@"Documents"];
NSString *libraryPath = [homePath stringByAppendingPathComponent:@"Library"];

NSLog(@"documentsPath:%@", documentsPath);
NSLog(@"libraryPath:%@", libraryPath);
}

{
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
NSString *libraryCachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;

NSLog(@"documentsPath:%@", documentsPath);
NSLog(@"libraryPath:%@", libraryPath);
NSLog(@"libraryCachePath:%@", libraryCachePath);
}

{
NSString *cachePath = [NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask].lastObject;
}

//上面写法,结果是一样的。

注意:NSSearchPathForDirectoriesInDomains函数的最后一个参数必须为YES。

注意:不要想着保存获取到的沙盒路径结果用于下一次使用,因为沙盒路径每次启动都会变化。使用上一次的结果是没法访问到你保存的文件的,你只需要保存相对路径比如:/Library/MyFolder/xxx.file。下一次取的时候使用NSHomeDirectory拼接即可。

参考

关于 iOS 删除缓存的那些事儿

官方文档

简要笔记

1
2
3
4
5
6
7
-沙盒
-xxx.app app的bundle,只读。
-Documents 存放用户产生的文件,比如IM数据库,用户的作品
-Library 存放非用户产生的文件,比如日志文件
-Caches 缓存文件,比如图片,视频等等,内存不足会被系统删除
-Preferences NSUserDefaults的kv
-Temp 存放临时文件,比如下载资源时的临时存放,内存不足会被系统删除
觉得文章有帮助可以打赏一下哦!