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

1、IOS规定每个应用程序都存放在自己的沙盒中;

2、应用程序不能够随意跨越自己的沙盒去访问别的应用程序的沙盒里面的内容;

3、当我们需要用到某些外部数据时,我们需要提出权限申请,得到允许才能向外请求数据或者接收数据。

ios读取手机应用 ios读取手机应用程序_沙盒

模拟器的沙盒文件夹在mac电脑上是被隐藏的,要想查看我们可以尝试如下步骤:

首先单击桌面背景->前往->前往文件夹->输入/Users/username/Library/Application Support/iPhone Simulator/  前往

ios读取手机应用 ios读取手机应用程序_ios读取手机应用_02

此处注意:username为你的用户名,根据自己的用户名来更改,最好参考下面代码打印结果来判断

打开之后我们可以看到如下文件:(此处将显示方式切换到第三种)

ios读取手机应用 ios读取手机应用程序_沙盒_03

ios读取手机应用 ios读取手机应用程序_sandbox_04

我们可以看到默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,每个应用只能在几个目录下读写文件
Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;

Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除

tmp:提供一个即时创建临时文件的地方。


还有一个.app文件,这个就是可运行的应用文件


新建一个test项目:MJ前缀然后在MJViewController.m文件中编写如下代码:



//取得test的家目录
 NSString *homeDir = NSHomeDirectory();
    NSLog(@"homeDir is %@",homeDir);
    //homeDir is /Users/admin/Library/Application Support/iPhone Simulator/7.1/Applications/8172820F-E2BA-4E8B-A2D6-B27AE34849A8
     
    //取得appDir的目录,可执行文件
 NSString *appDir = [[NSBundle  mainBundle] bundlePath];
    NSLog(@"appDir is %@",appDir);
    //appDir is /Users/admin/Library/Application Support/iPhone Simulator/7.1/Applications/8172820F-E2BA-4E8B-A2D6-B27AE34849A8/test.app
     
    //取得Document所在的目录
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
 NSString *path = [paths objectAtIndex:0];
    NSLog(@"ducoment dir is %@",path);
     
    //取得Library所在的位置
    paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES);
objectAtIndex:0];
    NSLog(@"ducoment dir is %@",path);
    //获取cache的目录
    paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
objectAtIndex:0];
     
    NSLog(@"tmp dir %@",NSTemporaryDirectory());//取得tmp的路径
     
    NSString *pic1 = [[NSBundlemainBundle] pathForResource:@"iconpng"ofType:@"png"];
    NSLog(@"iconpng path is %@",pic1);
    //iconpng path is /Users/admin/Library/Application Support/iPhone Simulator/7.1/Applications/8172820F-E2BA-4E8B-A2D6-B27AE34849A8/test.app/iconpng.png
     
    NSString *pic2 = [[NSBundlemainBundle] pathForResource:@"iconpng-2.png"ofType:nil];
    NSLog(@"iconpng-2 path is %@",pic2);
    //iconpng-2 path is /Users/admin/Library/Application Support/iPhone Simulator/7.1/Applications/8172820F-E2BA-4E8B-A2D6-B27AE34849A8/test.app/iconpng-2.png

    

    //总结:NSbundle就是取得test.app里面内容的一种资源方式


 //下面我们尝试向里面写入文件:

NSArray *docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [docPathobjectAtIndex:0];
    //NSString *documentsPath = [NSHomeDirectory() stringByAppendingString:@"/Documents"];
NSDocumentationDirectory方法路径一直出错才想出的方法大家可以忽略
 NSLog(@"documentsPath = %@",documentsPath);
    //写入文件
 if
 NSLog(@"目录没有找到!");
else{
 NSString *filePath = [documentsPath stringByAppendingPathComponent:@"/test.txt"];
 NSArray *array = [NSArray  arrayWithObjects:@"Title",@"Content",nil];
 writeToFile:filePath atomically:YES];
    }

    

    //读取文件

NSString *readPath = [documentsPath stringByAppendingString:@"/test.txt"];
 NSLog(@"%@",readPath);
    NSString *fileContent = [NSStringstringWithContentsOfFile:readPath encoding:NSUTF8StringEncodingerror:nil];
 NSLog(@"文件内容:%@",fileContent);



以上为代码段,新建test.txt之后我们可以查看文件目录:

ios读取手机应用 ios读取手机应用程序_ios读取手机应用_05

下面为输出结果:

ios读取手机应用 ios读取手机应用程序_sandbox_06

希望能对大家有所帮助,共同进步!