现在的位置: 首页 > 综合 > 正文

iOS下的文件和文件夹的隐藏

2018年02月13日 ⁄ 综合 ⁄ 共 1140字 ⁄ 字号 评论关闭

在实际的开发过程中,有些情况下,需要将文件或者文件夹隐藏起来,不让用户看到。

比如说在打开documents的共享的时候,又不希望用户通过itunes看到的情况下。

隐藏文件,其实是利用unix文件系统的特性,在文件命名的时候加了一个点“.”实现了隐藏文件的效果。

例如:创建了一个隐藏文件夹hideDir,之后在里边保存了一个文件passwrod.txt

代码如下:

[plain] view
plain
copy

  1. NSString* cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];  
  2.   NSString* hideDirpath = [NSString stringWithFormat:@"%@%@/", cachesPath,@"/.hideDir"];  
  3.   NSFileManager* fileManager = [NSFileManager defaultManager];  
  4.   BOOL isDir;  
  5.   if (![fileManager fileExistsAtPath:hideDirpath isDirectory:&isDir]) {  
  6.       if (![fileManager createDirectoryAtPath:hideDirpath withIntermediateDirectories:YES attributes:nil error:nil]) {  
  7.           NSLog(@"创建文件目录失败!");  
  8.       }  
  9.   }  
  10.     
  11.   NSString* filepath = [NSString stringWithFormat:@"%@%@",hideDirpath, @"password.txt"];  
  12.   NSDictionary * dic = [NSDictionary dictionaryWithObject:@"pwd1"forKey:@"pwd"];  
  13.   [dic writeToFile:filepath atomically:YES];  
  14.     
  15.   //验证一下,是否保存成功  
  16.   NSDictionary * context= [NSDictionary dictionaryWithContentsOfFile:filepath];  
  17.   NSLog(@"context=%@",context);

转自:http://blog.csdn.net/yuquan0821/article/details/7565976

抱歉!评论已关闭.