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

1、IOS沙盒机制

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

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

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

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

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

2、打开模拟器沙盒目录

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

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

2.1 方法1、可以设置显示隐藏文件,然后在Finder下直接打开。设置查看隐藏文件的方法如下:打开终端,输入命名

显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true

隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false

输完单击Enter键,退出终端,

重新启动Finder就可以了重启Finder:鼠标单击窗口左上角的苹果标志-->强制退出-->Finder-->

现在能看到资源库文件夹了。

打开资源库后找到/Application Support/iPhone Simulator/文件夹。这里面就是模拟器的各个程序的沙盒目录了。

2.2 方法2、这种方法更方便,在Finder上点->前往->前往文件夹,输入/Users/username/Library/Application Support/iPhone Simulator/ 前往。

username这里写你的用户名。

3、目录结构

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

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

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

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

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

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

iPhone在重启时,会丢弃所有的tmp文件。

我们创建一个IosSandbox的项目来展开沙盒和文件读写等操作的练习。

创建后找到模拟器上对应的目录,

这是目录全展开了。

这是上面提到的三个目录 :Documents、Library、 tmp

下篇介绍目录路径获取和文件操作

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

我们看看如何获取应用程序沙盒目录。包括真机的沙盒的目录。

1、获取程序的Home目录

    NSString *homeDirectory = NSHomeDirectory();
NSLog(@"path:%@", homeDirectory);

打印结果:

2012-06-17 14:00:06.098 IosSandbox[3536:f803] /Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/
Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2

那在真机上的目录有是怎么样的呢?我们看看

2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2

可见,真机上的目录是/var/mobile/Applications/这个目录下的,和模拟器不一样。这个是Home目录,其他的子目录和模拟器一样。

2、获取document目录

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);

打印结果

2012-06-17 14:00:06.099 IosSandbox[3536:f803] path:/Users/rongfzh/Library/Application Support/iPhone Simulator/5.1
/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Documents

3、获取Cache目录

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"%@", path);

打印结果:

2012-06-17 14:03:50.431 IosSandbox[3628:f803] /Users/rongfzh/Library/Application Support/iPhone Simulator/5.1
/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Library/Caches

4、获取Library目录

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"%@", path);

打印结果:

2012-06-17 14:07:17.544 IosSandbox[3733:f803] /Users/rongfzh/Library/Application Support/iPhone Simulator/5.1
/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Library

5、获取Tmp目录

   NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"%@", tmpDir);

打印结果:

2012-06-17 14:08:07.824 IosSandbox[3782:f803] /var/folders/g7/246bh79130zblw0yjjtc55cw0000gn/T/

6、写入文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
if (!docDir) {
NSLog(@"Documents 目录未找到");
}
NSArray *array = [[NSArray alloc] initWithObjects:@"内容",@"content",nil];
NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
[array writeToFile:filePath atomically:YES];

注:我们在真机上也运行一下,把文件写入,下一步从真机上把内容读取出来。

写入输入 array ,里面是两个字符串,一会我们读出来打印。

写入我们在程序沙盒目录下看到文件 testFile.txt

打开文件看到的内容是这样的,是个xml格式的plist文件,数据格式保存了内容。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>内容</string>
<string>content</string>
</array>
</plist>

7、读取文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
NSLog(@"%@", array);

打印结果:

把上面的文件解析后,把内容打印出来了。

2012-06-17 14:14:46.249 IosSandbox[3918:f803] (
"\U5185\U5bb9",
content
)

真机上读取并打印文件路径:

2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Documents/testFile.txt

(

"\U5185\U5bb9",

content

)

真机上也能写入和打印。

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

我们看看NSFileManager如何使用。包括创建文件,目录,删除,遍历目录等。

1、在Documents里创建目录

创建一个叫test的目录,先找到Documents的目录,

 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];

启动程序,这时候目录就创建了:

2、在test目录下创建文件

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

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

实现代码如下:

NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];
NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test22.txt"];
NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test33.txt"]; NSString *string = @"写入内容,write String";
[fileManager createFileAtPath:testPath contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
[fileManager createFileAtPath:testPath2 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
[fileManager createFileAtPath:testPath3 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

看下面的图,三个文件都出来了,内容也对。

在Documents目录下创建就更简单了,不用加test就ok了

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文件夹里的文件名

打印结果

2012-06-17 23:23:19.684 IosSandbox[947:f803] fileList:(
".DS_Store",
"test00.txt",
"test22.txt",
"test33.txt"
)
2012-06-17 23:23:19.686 IosSandbox[947:f803] fileLit(
".DS_Store",
"test00.txt",
"test22.txt",
"test33.txt"
)

两个方法都可以,隐藏的文件也打印出来了。

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];
[fileManager createFileAtPath:fileName contents:array attributes:nil];

这样就创建了testFileNSFileManager.txt并把三个hello world写入文件了

changeCurrentDirectoryPath目录更改到当前操作目录时,做文件读写就很方便了,不用加上全路径

5、删除文件

接上面的代码,remove就ok了。

[fileManager removeItemAtPath:fileName error:nil];

6、混合数据的读写

用NSMutableData创建混合数据,然后写到文件里。并按数据的类型把数据读出来

6.1写入数据:

    NSString * fileName = @"testFileNSFileManager.txt";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//获取文件路径
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
//待写入的数据
NSString *temp = @"nihao 世界";
int dataInt = 1234;
float dataFloat = 3.14f;
//创建数据缓冲
NSMutableData *writer = [[NSMutableData alloc] init];
//将字符串添加到缓冲中
[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
//将其他数据添加到缓冲中
[writer appendBytes:&dataInt length:sizeof(dataInt)];
[writer appendBytes:&dataFloat length:sizeof(dataFloat)];
//将缓冲的数据写入到文件中
[writer writeToFile:path atomically:YES];

我们看看数据怎么样了:

我们看到后面的是乱码,那是中文被转成了NSData后,还有int float的二进制

6.2读取刚才写入的数据:

 //读取数据:
int intData;
float floatData = 0.0;
NSString *stringData; NSData *reader = [NSData dataWithContentsOfFile:path];
stringData = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])]
encoding:NSUTF8StringEncoding];
[reader getBytes:&intData range:NSMakeRange([temp length], sizeof(intData))];
[reader getBytes:&floatData range:NSMakeRange([temp length] + sizeof(intData), sizeof(floatData))];
NSLog(@"stringData:%@ intData:%d floatData:%f", stringData, intData, floatData);

打印出来的结果:

2012-06-17 23:51:14.723 IosSandbox[1285:f803] stringData:nihao hello! intData:1234332 floatData:3.140000

这里把写入的汉字改成了 hello。因为[temp length]算长度是,把中文算成一位了,出来的结果有误。

IOS学习之IOS沙盒(sandbox)机制和文件操作的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. iOS 沙盒(sandbox)机制和文件操作

    本文参看了 http://www.uml.org.cn/mobiledev/201209211.asp#1 这篇文章中的介绍,尊重原著. 1.IOS沙盒机制 IOS应用程序只能在本应用程序中创建的文件 ...

  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. 【转】发布python的包至pypi服务器

    [原文链接]http://yejinxin.github.io/distribute-python-packages-to-pypi-server/ 使用pip或easy_install可以管理和安装 ...

  2. C++第一章概述

    1:C++主要是对于C的继承性做的相当的出色,主要扩充在于程序员可以自己定义自己的数据结构,用数据结构去描述日常生活中的事务,而不是C语言中当当仅有的Struct数据类型等等 2: 每一种语言都有自己 ...

  3. 自定义Camera综述(一般步骤、注意事项、遇到的难题<【内存溢出问题】>、像素参考)

    一般步骤: 1. 检查和访问Camera:创建代码来检查Camera和所申请访问的存在性: 2. 创建一个预览类:继承SurfaceView来创建一个Camera的预览类,并实现SurfaceHold ...

  4. SCAU 07校赛 10317 Fans of Footbal Teams

    10317 Fans of Footbal Teams 时间限制:1000MS  内存限制:65535K 题型: 编程题   语言: 无限制 Description Two famous footba ...

  5. Linux内存中的Cache真的能被回收么?

    在Linux系统中,我们经常用free命令来查看系统内存的使用状态.在一个RHEL6的系统上,free命令的显示内容大概是这样一个状态: [root@tencent64 ~]# free       ...

  6. 【转】 Nginx系列(一)--nginx是什么?

    原博文出于:http://blog.csdn.net/liutengteng130/article/details/46700939  感谢! 一.介绍 Nginx是一个高性能的HTTP和反向代理服务 ...

  7. 现代程序设计——homework-10

    设计 对于MVC我的理解是这样的,V是台显示器,注意仅仅是一台比显示器普通显示器多几个按钮,用户按什么,按了什么该干什么都不用操心:M是实体的软件抽象,假设实体可以但不执行,我就可以一步一步走,实体可 ...

  8. Gym 100818I Olympic Parade(位运算)

    Olympic Parade http://acm.hust.edu.cn/vjudge/contest/view.action?cid=101594#problem/I [题意]: 给出N个数,找出 ...

  9. C语言break语句

    break语句不能用于循环语句和switch语句之外的任何其他语句中: breakh中断switch break如果用于循环,是用来终止循环:break如果用于switch,则是用于终止switch. ...

  10. Hibernate3注解

    1.@Entity(name="EntityName") 必须,name为可选,对应数据库中一的个表 2.@Table(name="",catalog=&quo ...