本文参看了 http://www.uml.org.cn/mobiledev/201209211.asp#1 这篇文章中的介绍,尊重原著。


1、IOS沙盒机制

IOS应用程序只能在本应用程序中创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等。

1.1、每个应用程序都有自己的存储空间

1.2、应用程序不能翻过自己的围墙去访问别的存储空间的内容

1.3、应用程序请求的数据都要通过权限检测,假如不符合条件的话,不会被放行。

通过这张图只能从表层上理解sandbox是一种安全体系,应用程序的所有操作都要通过这个体系来执行,其中核心内容是:sandbox对应用程序执行各种操作的权限限制。


2、打开模拟器沙盒目录

下面看看模拟器的沙盒文件夹在mac电脑上的什么位置。

文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,英文名是Library。

下面介绍一种简单方法前往该文件夹:在Finder上点->前往->前往文件夹

进入模拟器后,里面就包含了各个应用程序的沙盒。

进入一个应用程序,如下图,就是一个沙箱了。

下面介绍一下沙箱的目录结构:

默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp和一个应用程序文件(也是一个文件)。因为应用的沙盒机制,应用只能在几个目录下读写文件

Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录

Library:存储程序的默认设置或其它状态信息;

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

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

iTunes在与iPhone同步时,备份所有的Documents和Library文件。

iPhone在重启时,会丢弃所有的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文件查看我们也可以到沙箱中进行查看。如下图:

3、获取沙盒目录:

  //1、获取程序的Home目录
NSString *homeDirectory = NSHomeDirectory();
NSLog(@"path:%@", homeDirectory);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671 //2、获取document目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671/Documents //3、获取Cache目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671/Library/Caches //4、获取Library目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671/Library //5、获取tmp目录
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"path:%@", tmpDir);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671/tmp/

4、文件操作之NSFileManager


4.1 、在document中创建一个文件目录

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"documentsDirectory%@",documentsDirectory);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
// 创建目录
[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];


4.2 、 在test目录下创建文件

创建文件怎么办呢?接着上面的代码 testPath 要用stringByAppendingPathComponent拼接上你要生成的文件名,比如test11.txt。这样才能在test目录下写入文件。

testDirectory是上面代码生成的路径哦,不要忘了。我往test文件夹里写入三个文件,test11.txt ,test22.txt,text.33.txt。内容都是写入内容,write String。

实现代码如下:

 NSString *testPath1 = [testDirectory stringByAppendingPathComponent:@"test1.txt"];
NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test2.txt"];
NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test3.txt"]; NSString *string = @"写入内容,write String"; [fileManager createFileAtPath:testPath1 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
[fileManager createFileAtPath:testPath2 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
[fileManager createFileAtPath:testPath3 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

4.3获取目录列里所有文件名

两种方法获取:subpathsOfDirectoryAtPath 和 subpathsAtPath

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"documentsDirectory%@",documentsDirectory);
NSFileManager *fileManage = [NSFileManager defaultManager];
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
//方法一
NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];
NSLog(@"%@",file);
//方法二
NSArray *files = [fileManage subpathsAtPath: myDirectory ];
NSLog(@"%@",files);

获取刚才test目录下的所以文件名:

两种方法都是输出

(
"test1.txt",
"test2.txt",
"test3.txt"
)

4.4 
、fileManager使用操作当前目录

//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//更改到待操作的目录下
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; //创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
NSString * fileName = @"testFileNSFileManager.txt";
NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil]; //下面是将数组类型转换为NSData类型
NSMutableData *data = [[NSMutableData alloc] init];
for (int i = 0; i < [array count]; ++i ){
NSString *str = [array objectAtIndex:i];
NSData *temp = [str dataUsingEncoding:NSUTF8StringEncoding];
[data appendData:temp];
}
//注意contents参数的类型是NSData类型
[fileManager createFileAtPath:fileName contents:data attributes:nil];


4.5 删除文件

接着上面的代码就可以将刚新建的 testFileNSFileManager.txt文件删除!

    [fileManager removeItemAtPath:fileName error:nil];

4.6 混合数据的读写  请参看原文最后面的内容。

大概就是这么多了吧!

iOS 沙盒(sandbox)机制和文件操作的更多相关文章

  1. IOS学习之IOS沙盒(sandbox)机制和文件操作

    IOS学习之IOS沙盒(sandbox)机制和文件操作(一) 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都 ...

  2. iOS学习之iOS沙盒(sandbox)机制和文件操作1

    iOS学习之iOS沙盒(sandbox)机制和文件操作 接上篇 iOS学习之iOS沙盒(sandbox)机制和文件操作(一) 我们看看如何获取应用程序沙盒目录.包括真机的沙盒的目录. 1.获取程序的H ...

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

    IOS学习之IOS沙盒(sandbox)机制和文件操作   作者:totogo2010 ,发布于2012-9-21,来源:CSDN   目录: IOS学习之IOS沙盒(sandbox)机制和文件操作( ...

  4. iOS学习之iOS沙盒(sandbox)机制和文件操作(一)

    1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...

  5. iOS学习之iOS沙盒(sandbox)机制和文件操作复习

    1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...

  6. iOS学习之iOS沙盒(sandbox)机制和文件操作(二)

    1.获取程序的Home目录 NSString *homeDirectory = NSHomeDirectory(); NSLog(@"path:%@", homeDirectory ...

  7. iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager(三)

    1.在Documents里创建目录 创建一个叫test的目录,先找到Documents的目录, NSArray *paths = NSSearchPathForDirectoriesInDomains ...

  8. iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager

    我们看看NSFileManager如何使用.包括创建文件,目录,删除,遍历目录等. 1.在Documents里创建目录 创建一个叫test的目录,先找到Documents的目录, [cpp] view ...

  9. iOS学习7:iOS沙盒(sandBox)机制(一)之获取沙盒路径及目录说明(转)

    转:http://my.oschina.net/joanfen/blog/151145 一.iOS沙盒机制 iOS的应用只能访问为该应用创建的区域,不可访问其他区域,应用的其他非代码文件都存在此目录下 ...

随机推荐

  1. 2016年7月2日 星期六 --出埃及记 Exodus 14:29

    2016年7月2日 星期六 --出埃及记 Exodus 14:29 But the Israelites went through the sea on dry ground, with a wall ...

  2. android 入门 003 (点击事件)

    点击事件 有四种实现方式. 1.内部类实现方式 1.0 package cn.rfvip.clickevent; import android.app.Activity; import android ...

  3. BZOJ 2758 Blinker的噩梦(扫描线+熟练剖分+树状数组)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2758 题意:平面上有n个多边形(凸包和圆).任意两个多边形AB只有两种关系:(1) ...

  4. BZOJ 1266 上学路线route(最小割)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1266 题意:给出一个无向图,每条边有长度和代价.求出1到n的最短路.之后删掉一些边使得1 ...

  5. DISPLAY_ITEM built-in in Oracle D2k Forms

    DISPLAY_ITEM built-in in Oracle D2k Forms DescriptionMaintained for backward compatibility only. For ...

  6. HQL语句大全(转载)

    Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL.但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可以理解如继承.多态 和关联之类的概念. ...

  7. 【转】利用Pspice分析放大器环路的稳定性

    文章来源: http://www.21ic.com/app/test/201108/90808.htm 虽然在较低频率下可以较轻松地检查一个简单放大器的稳定性,但评估一个较为复杂的电路是否稳定,难度可 ...

  8. LINUX DIFF命令详解

    刚才在和公司做离线IP对比,最后手工了,感觉还是比较麻烦的,遇到数据很大的时候不能手工进行了 本想用linux下的DIFF来进行对比,发现结果很乱.时间很紧最后还是手工了. 现在忙完要认认真真学习一下 ...

  9. TestNg测试框架使用笔记

    Gradle支持TestNG test { useTestNG(){ //指定testng配置文件 suites(file('src/test/resources/testng.xml')) } } ...

  10. E2 2014.08.05 更新日志

    增加功能 增加手机.平板兼容模块,用手机平板也能正常登陆和使用软件 介绍  演示 对数据库全面优化,全面提升数据量很大时统计分析的性能 完善功能 销售分析增加按商品分类分析 完善客户明细窗口的客户信息 ...