认识沙盒
何为沙盒?沙盒装什么?沙盒:系统为iOS应用创建的非此应用不可读写的文件夹,每个iOS应用都有一个和它一一对应的沙盒,每个应用且只能访问自己的沙盒;沙盒里装了此应用的非代码所有文件。
通过代码在控制台输出沙盒路径:
NSLog(@"%@",NSHomeDirectory());
然后在Finder中点击“前往文件夹”,输入控制台打印的沙盒路径,前往直达沙盒
沙盒三大块
- Documents
保存应用需持久化的数据,iTunes会备份此文件夹
路径:
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@",documentsPath);
- Library(它下面包含Caches,Preferences)
iTunes不会备份此文件夹- Caches
缓存区域(放一些图片文字啊这些需要缓存的东西)
路径:
- Caches
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@",cachesPath);
- Preferences
应用默认设置(用户偏好设置,应用初始设置)
路径:
NSString *preferencePath = [NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@",preferencePath);
- tmp
保存应用运行时所需的临时文件,随时有删除的可能,iTunes不会备份此文件夹
路径:
NSString *temPath = NSTemporaryDirectory();
NSLog(@"%@",temPath);
