一、沙盒
每个IOS程序都有自己的程序目录(沙盒),与其他系统文件隔离,应用只能访问自己的沙盒(IOS8中已经开放)
应⽤沙盒的文件系统⽬录,如下图所示(假设应用的名称叫Layer)
模拟器应⽤用沙盒的根路径在: (city--online是⽤用户名)
/Users/city--online/Library/Developer/CoreSimulator/Devices/18B16F4E-BB49-41F1-90AE-842283E72C8C/data/Containers/Data/Application/D81121AC-377F-487B-A844-AC3A313D495C
Documents:保存应⽤运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录
tmp:保存应⽤运行时所需的临时数据,使⽤完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同步设备时 不会备份该目录
Library/Caches:保存应用运行时⽣成的需要持久化的数据,iTunes同步设备时不会备份该目录。⼀一般存储体积大、不需要备份的非重要数据
Library/Preference:保存应用的所有偏好设置,iOS的Settings(设置) 应⽤会在该⺫录中查找应⽤的设置信息。iTunes同步设备时会备份该目录
// // ViewController.m // Plist // // Created by City--Online on 15/4/21. // Copyright (c) 2015年 CYW. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //获取沙盒根目录 NSString *home=NSHomeDirectory(); NSLog(@"沙盒根目录:%@\n\n",home); //获取Documents目录 不建议采用 NSString *documents=[home stringByAppendingPathComponent:@"Documents"]; NSLog(@"字符串拼接获取Documents:%@\n\n",documents); //NSUserDomainMask 代表从用户文件夹下找 //YES 代表展开路径中的波浪字符“~” NO ~/Documents NSArray *array=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO); // 在iOS中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素 NSString *documents1=[array objectAtIndex:0]; NSLog(@"通过方法NSSearchPathForDirectoriesInDomains获取Documents:%@\n\n",documents1); //获取tmp文件目录 NSLog(@"tmp 文件目录:%@\n\n",NSTemporaryDirectory()); //获取Library/Caches: NSArray *arrayCaches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSLog(@"Library/Caches:%@",arrayCaches[0]); //Library/Preference:通过NSUserDefaults类存取该目录下的设置信息 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
输出:
2015-04-21 13:44:39.565 Plist[2829:130133] 沙盒根目录:/Users/city--online/Library/Developer/CoreSimulator/Devices/18B16F4E-BB49-41F1-90AE-842283E72C8C/data/Containers/Data/Application/1F25A9BC-BB1D-4F5D-88F1-A96E9E0E9EAE 2015-04-21 13:44:39.566 Plist[2829:130133] 字符串拼接获取Documents:/Users/city--online/Library/Developer/CoreSimulator/Devices/18B16F4E-BB49-41F1-90AE-842283E72C8C/data/Containers/Data/Application/1F25A9BC-BB1D-4F5D-88F1-A96E9E0E9EAE/Documents 2015-04-21 13:44:39.566 Plist[2829:130133] 通过方法NSSearchPathForDirectoriesInDomains获取Documents:~/Documents 2015-04-21 13:44:39.566 Plist[2829:130133] tmp 文件目录:/Users/city--online/Library/Developer/CoreSimulator/Devices/18B16F4E-BB49-41F1-90AE-842283E72C8C/data/Containers/Data/Application/1F25A9BC-BB1D-4F5D-88F1-A96E9E0E9EAE/tmp/ 2015-04-21 13:44:39.567 Plist[2829:130133] Library/Caches:/Users/city--online/Library/Developer/CoreSimulator/Devices/18B16F4E-BB49-41F1-90AE-842283E72C8C/data/Containers/Data/Application/1F25A9BC-BB1D-4F5D-88F1-A96E9E0E9EAE/Library/Caches