`
R任轩
  • 浏览: 14861 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

IOS沙盒机制(SandBox)

 
阅读更多

IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容。所有的非代码文件都保存在这个地方,比如图片、声音、属性列表和文本文件等。

1.每个应用程序都在自己的沙盒内

2.不能随意跨越自己的沙盒去访问别的应用程序沙盒的内容

3.应用程序向外请求或接收数据都需要经过权限认证

 

查看模拟器的沙盒文件夹在Mac电脑上的存储位置,首先,这个文件夹是被隐藏的,所以要先将这些文件显示出来,打开命令行:

 

显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true

 

隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false

 

然后重新启动Finder,点击屏幕左上角苹果标志——强制退出——选择Finder然后点击重新启动,这个时候在重新打开Finder就可以看到被隐藏的文件了。

 

还有一种比较简单的办法就是直接点击Finder图标右键——前往文件夹——输入/Users/your username/Library/Application Support/iPhone Simulator/ ,然后确认就可以了。your username是你本机的用户名

一、进入相应的文件夹,就可以到模拟器的沙盒文件目录

二、接着进入一个模拟器版本

三、然后可以看到Applications下面存放的就是模拟器中所装的开发的应用程序,随便进入一个后可以看到,一个沙盒中包含了四个部分:分别是.app文件,这个就是可运行的应用文件,Documents,苹 果建议将程序中创建的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录;Library,存储程序的默认设置或其它 状态信息;Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除;tmp,创建和存放临时文件的地 方。

下面通过代码来获取这些目录:

//获取根目录  
 2         NSString *homePath = NSHomeDirectory();  
 3         NSLog(@"Home目录:%@",homePath);  
 4           
 5         //获取Documents文件夹目录,第一个参数是说明获取Doucments文件夹目录,第二个参数说明是在当前应用沙盒中获取,所有应用沙盒目录组成一个数组结构的数据存放  
 6         NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
 7         NSString *documentsPath = [docPath objectAtIndex:0];  
 8         NSLog(@"Documents目录:%@",documentsPath);  
 9           
10         //获取Cache目录  
11         NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
12         NSString *cachePath = [cacPath objectAtIndex:0];  
13         NSLog(@"Cache目录:%@",cachePath);  
14           
15         //Library目录  
16         NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);  
17         NSString *libPath = [libsPath objectAtIndex:0];  
18         NSLog(@"Library目录:%@",libPath);  
19           
20         //temp目录  
21         NSString *tempPath = NSTemporaryDirectory();  
22         NSLog(@"temp目录:%@",tempPath);  

 下面开始向目录里面创建文件,然后向文件里面写入内容:

NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
 2         NSString *documentsPath = [docPath objectAtIndex:0];  
 3         //写入文件  
 4         if (!documentsPath) {  
 5             NSLog(@"目录未找到");  
 6         }else {  
 7             NSString *filePaht = [documentsPath stringByAppendingPathComponent:@"test.txt"];  
 8             NSArray *array = [NSArray arrayWithObjects:@"Title",@"Contents", nil];  
 9             [array writeToFile:filePaht atomically:YES];  
10         }  

 创建成功后打开文件夹目录,可以看到test.txt文件

接下来是把该文件中的内容读出来:

//读取文件  
2     NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
3         NSString *documentsPath = [docPath objectAtIndex:0];  
4         NSString *readPath = [documentsPath stringByAppendingPathComponent:@"test.txt"];  
5         NSArray *fileContent = [[NSArrayalloc] initWithContentsOfFile:readPath];  
6         NSLog(@"文件内容:%@",fileContent);  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics