天天看点

IOS沙盒(sandbox)机制和文件操作

转自:iOS学习之iOS沙盒(sandbox)机制和文件操作(二)

沙盒含有4个文件夹:Documents, Library,tmp以及应用的App文件。

documents: 只有用户生成的文件、其他数据及其他程序不能重新创建的文件,并将通过iCloud自动备份。

library-caches: 可以重新下载或者重新生成的数据。举个例子,比如杂志、新闻、地图应用使用的数据库缓存文件和可下载内容应该保存到这个文件夹。

library-preferences:存放NSUserDefault的plist数据属于程序的默认设置或是其他状态(偏好)信息。

temp:存放就是临时数据,这些数据再程序关闭后当第二次打开时会消失。

App文件:包含了应用程序本身的数据,包括资源文件(图片等)和可执行文件等,只读。

 0、ios读取app文件

NSLog(@"%@",[[NSBundle mainBundle] bundlePath]);
NSString *dataFilePath = [[NSBundle mainBundle] pathForResource:@"readme" ofType:@"txt"]; 
           

我们看看如何获取应用程序沙盒目录。包括真机的沙盒的目录。

1、获取程序的Home目录(沙盒目录)

[cpp]  view plain copy

  1. NSString *homeDirectory = NSHomeDirectory();  
  2. NSLog(@"path:%@", homeDirectory);  

打印结果:

[cpp]  view plain copy

  1. 2012-06-17 14:00:06.098 IosSandbox[3536:f803] /Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2  

那在真机上的目录有是怎么样的呢?我们看看

2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2

可见,真机上的目录是/var/mobile/Applications/这个目录下的,和模拟器不一样。这个是Home目录,其他的子目录和模拟器一样。

2、获取document目录 [cpp]  view plain copy

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2. NSString *path = [paths objectAtIndex:0];  
  3. NSLog(@"path:%@", path);  

打印结果

[cpp]  view plain copy

  1. 2012-06-17 14:00:06.099 IosSandbox[3536:f803] path:/Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Documents  

3、获取Cache目录

[cpp]  view plain copy

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  2. NSString *path = [paths objectAtIndex:0];  
  3. NSLog(@"%@", path);  

打印结果

[cpp]  view plain copy

  1. 2012-06-17 14:03:50.431 IosSandbox[3628:f803] /Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Library/Caches  

4、获取Library目录

[cpp]  view plain copy

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);  
  2. NSString *path = [paths objectAtIndex:0];  
  3. NSLog(@"%@", path);  

打印结果 [cpp]  view plain copy

  1. 2012-06-17 14:07:17.544 IosSandbox[3733:f803] /Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Library  

5、获取Tmp目录 [cpp]  view plain copy

  1. NSString *tmpDir = NSTemporaryDirectory();  
  2.  NSLog(@"%@", tmpDir);  

打印结果

[cpp]  view plain copy

  1. 2012-06-17 14:08:07.824 IosSandbox[3782:f803] /var/folders/g7/246bh79130zblw0yjjtc55cw0000gn/T/  

6、写入文件 [cpp]  view plain copy

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2.     NSString *docDir = [paths objectAtIndex:0];  
  3.     if (!docDir) {  
  4.         NSLog(@"Documents 目录未找到");          
  5.     }  
  6.     NSArray *array = [[NSArray alloc] initWithObjects:@"内容",@"content",nil];  
  7.     NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];  
  8.     [array writeToFile:filePath atomically:YES];  

注:我们在真机上也运行一下,把文件写入,下一步从真机上把内容读取出来。

写入输入 array ,里面是两个字符串,一会我们读出来打印。

写入我们在程序沙盒目录下看到文件 testFile.txt

IOS沙盒(sandbox)机制和文件操作

打开文件看到的内容是这样的,是个xml格式的plist文件,数据格式保存了内容。

[cpp]  view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
  3. <plist version="1.0">  
  4. <array>  
  5.     <string>内容</string>  
  6.     <string>content</string>  
  7. </array>  
  8. </plist>  

7、读取文件

[cpp]  view plain copy

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2.     NSString *docDir = [paths objectAtIndex:0];  
  3.     NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];  
  4.     NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];  
  5.     NSLog(@"%@", array);  

打印结果:

把上面的文件解析后,把内容打印出来了。

[cpp]  view plain copy

  1. 2012-06-17 14:14:46.249 IosSandbox[3918:f803] (  
  2.     "\U5185\U5bb9",  
  3.     content  
  4. )  

真机上读取并打印文件路径:

2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Documents/testFile.txt

 (

    "\U5185\U5bb9",

    content

)

真机上也能写入和打印。