在实际的开发过程中,有些情况下,需要将文件或者文件夹隐藏起来,不让用户看到。
比如说在打开documents的共享的时候,又不希望用户通过itunes看到的情况下。
隐藏文件,其实是利用unix文件系统的特性,在文件命名的时候加了一个点“.”实现了隐藏文件的效果。
例如:创建了一个隐藏文件夹hideDir,之后在里边保存了一个文件passwrod.txt
代码如下:
NSString* cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString* hideDirpath = [NSString stringWithFormat:@"%@%@/", cachesPath,@"/.hideDir"];
NSFileManager* fileManager = [NSFileManager defaultManager];
BOOL isDir;
if (![fileManager fileExistsAtPath:hideDirpath isDirectory:&isDir]) {
if (![fileManager createDirectoryAtPath:hideDirpath withIntermediateDirectories:YES attributes:nil error:nil]) {
NSLog(@"创建文件目录失败!");
}
}
NSString* filepath = [NSString stringWithFormat:@"%@%@",hideDirpath, @"password.txt"];
NSDictionary * dic = [NSDictionary dictionaryWithObject:@"pwd1"forKey:@"pwd"];
[dic writeToFile:filepath atomically:YES];
//验证一下,是否保存成功
NSDictionary * context= [NSDictionary dictionaryWithContentsOfFile:filepath];
NSLog(@"context=%@",context);