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

IOS学习—沙盒机制(SandBox)

2013年10月22日 ⁄ 综合 ⁄ 共 5232字 ⁄ 字号 评论关闭

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是你本机的用户名

然后按下图进入相应的文件夹,就可以到模拟器的沙盒文件目录了:

接着进入一个模拟器版本,我这里是5.1


然后可以看到Applications下面存放的就是模拟器中所装的开发的应用程序,随便进入一个后可以看到,一个沙盒中包含了四个部分,如图所示:


分别是.app文件,这个就是可运行的应用文件,Documents,苹果建议将程序中创建的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录;Library,存储程序的默认设置或其它状态信息;Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除;tmp,创建和存放临时文件的地方。

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


[cpp] view
plain
copyprint?
//获取根目录   
    NSString *homePath = NSHomeDirectory();  
    NSLog(@"Home目录:%@",homePath);  
      
    //获取Documents文件夹目录,第一个参数是说明获取Doucments文件夹目录,第二个参数说明是在当前应用沙盒中获取,所有应用沙盒目录组成一个数组结构的数据存放   
    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
    NSString *documentsPath = [docPath objectAtIndex:0];  
    NSLog(@"Documents目录:%@",documentsPath);  
      
    //获取Cache目录   
    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
    NSString *cachePath = [cacPath objectAtIndex:0];  
    NSLog(@"Cache目录:%@",cachePath);  
      
    //Library目录   
    NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);  
    NSString *libPath = [libsPath objectAtIndex:0];  
    NSLog(@"Library目录:%@",libPath);  
      
    //temp目录   
    NSString *tempPath = NSTemporaryDirectory();  
    NSLog(@"temp目录:%@",tempPath);  

//获取根目录      NSString *homePath = NSHomeDirectory();      NSLog(@"Home目录:%@",homePath);            //获取Documents文件夹目录,第一个参数是说明获取Doucments文件夹目录,第二个参数说明是在当前应用沙盒中获取,所有应用沙盒目录组成一个数组结构的数据存放      NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);      NSString *documentsPath = [docPath objectAtIndex:0];      NSLog(@"Documents目录:%@",documentsPath);            //获取Cache目录      NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);      NSString *cachePath = [cacPath objectAtIndex:0];      NSLog(@"Cache目录:%@",cachePath);            //Library目录      NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);      NSString *libPath = [libsPath objectAtIndex:0];      NSLog(@"Library目录:%@",libPath);            //temp目录      NSString *tempPath = NSTemporaryDirectory();      NSLog(@"temp目录:%@",tempPath);  

输出结果如下:

2012-08-03 11:10:24.325 SandBoxTest[12549:f803] Home目录:/Users/Ryan/Library/Application Support/iPhone Simulator/5.1/Applications/A6B99E5A-E2C7-46E9-867A-4E7619F0DA45

2012-08-03 11:10:24.325 SandBoxTest[12549:f803] Documents目录:/Users/Ryan/Library/Application Support/iPhone Simulator/5.1/Applications/A6B99E5A-E2C7-46E9-867A-4E7619F0DA45/Documents

2012-08-03 11:10:24.326 SandBoxTest[12549:f803] Cache目录:/Users/Ryan/Library/Application Support/iPhone Simulator/5.1/Applications/A6B99E5A-E2C7-46E9-867A-4E7619F0DA45/Library/Caches

2012-08-03 11:10:24.326 SandBoxTest[12549:f803] Library目录:/Users/Ryan/Library/Application Support/iPhone Simulator/5.1/Applications/A6B99E5A-E2C7-46E9-867A-4E7619F0DA45/Library

2012-08-03 11:10:24.326 SandBoxTest[12549:f803] temp目录:/var/folders/7z/1wj5h8zx7b59c02pxmpynd500000gn/T/

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


[cpp] view
plain
copyprint?
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
    NSString *documentsPath = [docPath objectAtIndex:0];  
    //写入文件   
    if (!documentsPath) {  
        NSLog(@"目录未找到");  
    }else {  
        NSString *filePaht = [documentsPath stringByAppendingPathComponent:@"test.txt"];  
        NSArray *array = [NSArray arrayWithObjects:@"Title",@"Contents", nil];  
        [array writeToFile:filePaht atomically:YES];  
    }  

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


[cpp] view
plain
copyprint?
  


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

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


[cpp] view
plain
copyprint?
//读取文件   
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
    NSString *documentsPath = [docPath objectAtIndex:0];  
    NSString *readPath = [documentsPath stringByAppendingPathComponent:@"test.txt"];  
    NSArray *fileContent = [[NSArrayalloc] initWithContentsOfFile:readPath];  
    NSLog(@"文件内容:%@",fileContent);  

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


输出结果如下:

2012-08-03 11:26:53.594 SandBoxTest[12642:f803] 文件内容:(

    Title,

    Contents

)

原文地址:http://wuyunhe.5945.blog.163.com/blog/static/58979048201302381430331/

抱歉!评论已关闭.