本文参看了 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. hdu 2089 不要62 数位dp

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. 调用REST接口获取数据

    /// <summary> /// 根据机构代码本机构下报警用户列表: /// </summary> /// <param name="org_code&quo ...

  3. .Net六大验证及使用方法

    C#包含有六种验证方式,分别为: 一.非空验证  RequiredFieldValidator. 二.对比验证 CompareValidator. 三.范围验证 RangeValidator. 四.正 ...

  4. Create XO Checker Game With Oracle Forms

    Created XO Checker game in Oracle Forms and sharing its FMB (source code) for reference so that you ...

  5. How to run an manually installed program from terminals in Linux / Ubuntu

    Say we have installed qt programs and we want to run qtcreator from the command line. What we need h ...

  6. 一个ListBox的例子

    1.向ListBox中放入其他控件 XAML: <Window x:Class="ItemsControls.MainWindow" xmlns="http://s ...

  7. Android 布局简要范例

    Android的布局决定着实际的UI界面呈现情况,正是这些UI界面的组合与千变万化,才呈现出了各式各样的风格. 而这些基础的布局框架结构很重要,需要玩的很熟悉.我将以前参考的部分代码示例,所做的相关实 ...

  8. iOS - OC NSSize 尺寸

    前言 结构体,这个结构体用来表示事物的宽度和高度. typedef CGSize NSSize; struct CGSize { CGFloat width; CGFloat height; }; t ...

  9. iOS - MVP 架构模式

    1.MVP 从字面意思来理解,MVP 即 Modal View Presenter(模型 视图 协调器),MVP 实现了 Cocoa 的 MVC 的愿景.MVP 的协调器 Presenter 并没有对 ...

  10. c++ vector 简单实现。

    第二次修改: 1)熟悉基本的模板编程,头文件和定义必须放到一起. 2)熟悉内存管理模板类 allocator<T>. 1.使用标准库的内存管理类 allocator<T> 代替 ...