您的位置:首页 > 其它

沙盒机制--SandBox

2015-09-21 16:27 791 查看
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不会备份此目录,此目录下文件不会在应用退出删除; Library/Preferences:? tmp:创建和存放临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。

注意:这里很容易和bundle混淆在一起,下面根据自己的一点理解说明二者的区别:
bundle :生成 iOS 应用程序时,Xcode 将它捆绑成一个包。捆绑包 (bundle) 是文件系统中的一个目录,它将相关资源成组在一个地方。一个 iOS 应用程序捆绑包中,含有其可执行文件和支持资源文件(如应用程序图标、图像文件和已本地化的内容)。
A bundle(包裹、捆、束) is a directory with a standardizedhierarchical structure that holds executable code and the resources used by that code.
所以可以将整个应用程序其实就可以看做一个bundle。
沙箱的概念和bundle没直接关系,沙箱只是说明程序资源与外界隔离
这个问题对于新手来说很容易理解错误
下面通过一个简单的例子说明一下bundle和sandbox。
//新建的plist文件是在应用程序中的,可以通过bundle存取到该文件
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:plistPath];

//向数组中新添加一个项目
[array addObject:@"3"];
//重新写回plist文件中
BOOL value = [array writeToFile:plistPath atomically:YES];
if (value) {
NSMutableArray *newArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSLog(@"new array = %@",newArray);
}
/* 输出:
new array = (
0,
1,
2,
3
)
*/

//获取沙箱中document的path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

//将数组写入到沙箱的document中的data.plist文件中
[array writeToFile:newPath atomically:YES];

NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:newPath];
NSLog(@"array in data.plist = %@",arr);
/* 输出:
array in data.plist = (
0,
1,
2,
3
)
*/


说明:我们首先在项目中新建一个plist文件(root项的类型为数组),添加了3个元素。因为新建的plist文件是在应用程序中的,我们可以通过bundle获取到这个plist文件,读取出这个数组,添加一个数据元素后,重新写回plist文件中。接着我们获取沙箱document的path,然后将这个文件写入到沙箱中的data.plist文件中(如果不存在,会自动新建一个的),然后再从data.plist读取出这个数组。
关于新建的MyPlist.plist文件,我们写回文件的数组中添加了一项新的元素,但是我们在xcode中查看这个MyPlist.plist文件时,发现并没有显示出新增的数组元素,但是我们到沙箱中查看就可以看到了,这个估计是xoode本身的问题。关于document中data.plist文件查看我们也可以到沙箱中进行查看。如下图:




下面通过代码来获取这些目录:
//得到app包的路径
NSString *appPath = [[NSBundle mainBundle]resourcePath];

//获取根目录
NSString *homePath = NSHomeDirectory();
NSLog(@"Home目录:%@",homePath);


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

//获取Cache目录
NSArray *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
NSLog(@"Cache目录:%@",cachePath);

//Library目录
NSArray *libraryPath = 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/Documents2012-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/Library2012-08-03 11:10:24.326 SandBoxTest[12549:f803] temp目录:/var/folders/7z/1wj5h8zx7b59c02pxmpynd500000gn/T/下面开始向目录里面创建文件,然后向文件里面写入内容:

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];
}
创建成功后打开文件夹目录,可以看到test.txt文件:


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

//读取文件
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bundle sandBox