沙盒(二)
来源:互联网 发布:python无限循环语句 编辑:程序博客网 时间:2024/06/14 09:45
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>
打印结果:
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方法删除相应文件。
- 沙盒(二)
- iOS-沙盒(二)
- (二)
- (二):基二FFT
- 凤凰涅磐(二十-二十五)
- 凤凰涅磐(二十六-二十七)
- 软件中的八二原则(二)
- 二 迭代器(二)相关辅助函数
- 二.Java编程基础(二)
- java提高篇(二二)-----LinkedList
- java提高篇(二二)-----LinkedList
- 黑客编程入门(二。二)
- (二十)java小练习二
- (二十六)svn的问题二
- java提高篇(二二)-----LinkedList
- java提高篇(二二)-----LinkedList
- JAVA二学(二)剖析String
- 二、runtime之类和对象(二)
- 1px dotted 在IE6下不支持
- Hash算法学习(1)
- IE6下父级有边框的时候,子元素的margin值消失
- C++异常处理机制
- 将php数组传递到js—json_encode(),json_decode()
- 沙盒(二)
- hadoop的rpc同异步转换原理
- JFreeChar中文API
- perl写子程序持久性私有变量state声明符报错的原因
- <<The C Programming Language>>学习之路-练习题参考答案 1-11
- 2048 C语言版
- 计算机视觉 监测小组
- HDU 2095
- Java split用法