沙盒(二)
2014-03-31 22:49
375 查看
Foundation提供了C函数NSSearchPathForDirectoriesInDomains来访问沙盒目录下的各个文件夹。
1、获取documents目录
<span style="font-size:14px;">NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = paths[0]; NSLog(@"paths=%@,\n documentsDirectory=%@",paths,documentsDirectory);</span>
常量NSDocumentationDirectory表示我们正在查找documents目录的路径。第二个常量表示我们希望搜索的范围是在当前应用沙盒内。该方法返回值是一个数组,但是我们知道每一个应用只有一个documents文件夹,所以数组内只有一个元素,即paths[0]。
打印结果:
2014-03-31 22:33:38.312 Sandbox[587:60b] paths=(
"/Users/sunke/Library/Application Support/iPhone Simulator/7.1/Applications/8B44A2EF-04D5-4C9C-9DA7-A0D8737905A1/Library/Documentation"
),
documentsDirectory=/Users/sunke/Library/Application Support/iPhone Simulator/7.1/Applications/8B44A2EF-04D5-4C9C-9DA7-A0D8737905A1/Library/Documentation
2、获取tmp目录
<span style="font-size:14px;">NSString * tmpPath = NSTemporaryDirectory(); //在该路径结尾拼接上文件名就是该目录下的文件路径 NSString * testFile = [tmpPath stringByAppendingPathComponent:@"hello.txt"]; NSLog(@"testFile = %@",testFile);</span>
打印结果:
2014-03-31 22:42:24.985 Sandbox[623:60b] testFile = /Users/sunke/Library/Application Support/iPhone Simulator/7.1/Applications/8B44A2EF-04D5-4C9C-9DA7-A0D8737905A1/tmp/hello.txt
3、获取library目录
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString * libraryPath = [paths objectAtIndex:0]; NSLog(@"libraryPath = %@",libraryPath);
打印结果:
2014-03-31 22:46:36.674 Sandbox[641:60b] libraryPath = /Users/sunke/Library/Application Support/iPhone Simulator/7.1/Applications/8B44A2EF-04D5-4C9C-9DA7-A0D8737905A1/Library
4、创建自己的目录和文件保存路径
比如我们要在Document目录下创建一个子目录,然后将需要保存的数据写入文件到该子目录。<span style="font-size:14px;">- (BOOL)addToCache:(NSMutableArray *)array { /*获取document目录*/ NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentDirectory = [paths objectAtIndex:0]; NSFileManager * fileManger = [NSFileManager defaultManager]; /*创建目录*/ NSString * videoOnlineDirectory = [documentDirectory stringByAppendingPathComponent:@"VideoOnlineCache"]; [fileManger createDirectoryAtPath:videoOnlineDirectory withIntermediateDirectories:YES attributes:nil error:nil]; /*创建文件保存路径*/ NSString * fileName = self.title; NSString * filePath = [videoOnlineDirectory stringByAppendingPathComponent:fileName]; /*写入文件*/ NSData * data = [NSKeyedArchiver archivedDataWithRootObject:array]; BOOL flag = [data writeToFile:filePath atomically:YES]; NSLog(@"是否已经添加缓存 %d",flag); return flag; }</span>
addToCache方法中,首先获取了Document目录,然后通过fileManger的createDirectoryAtPath方法创建一个子目录,然后在该子目录下创建一个保存文件的路径filePath,最后,将需要保存的数据array归档写入该路径。
5、删除目录
有时候应用中的缓存占用空间比较大时,我们需要为用户提供清理缓存的入口。比如我将缓存保存在document目录下,现在需要清理该目录下所有缓存文件。NSFileManager* manager = [NSFileManager defaultManager]; NSString * folderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator]; NSString* fileName; while ((fileName = [childFilesEnumerator nextObject]) != nil){ NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName]; [manager removeItemAtPath:fileAbsolutePath error:nil]; }
通过subpathsAtPath获取documents目录下所有文件路径的数组,然后请求一个枚举器遍历文件路径并通过removeItemAtPath方法删除相应文件。
相关文章推荐
- 我的iPhone桌面
- “传奇”图象数据存储方式
- SQL Server误区30日谈 第18天 有关FileStream的存储,垃圾回收以及其它
- PHP 存储文本换行实现方法
- 注册表中存储数据库链接字符串的方法
- 深入PHP变量存储的详解
- Android App中各种数据保存方式的使用实例总结
- 我对块存储,文件存储,对象存储的认识
- Object-c Archiving归档
- 西蒙iphone-OpenGL ES教程英文原帖地址以及实例代码
- [开源项目]开心英语8000句 Iphone+Ipad版
- 月光微博客
- 月光微博客
- iPhone动画效果
- iPhone动画属性详解
- 获取crash report ,并解析定位代码
- 深入浅出全解SATA存储解决方案
- 在开发iOS程序时对日期处理的总结
- 在iPhone项目中使用讯飞语音SDK实现语音识别和语音合成