iOS学习之沙盒

时间:2022-03-19 13:21:56

1iOS沙盒

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

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

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

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

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同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录
  • tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同步设备时不会备份该目录
  • Library/Caches:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录。一般存储体积大、不需要备份的非重要数据
  • Library/Preference:保存应用的所有偏好设置,iOS的Settings(设置)应用会在该目录中查找应用的设置信息。iTunes同步设备时会备份该目录

二、获取主要目录路径的方式

1、沙盒目录

NSLog(@"%@",NSHomeDirectory()); 

输出结果:

/var/mobile/Applications/326640A7-6E27-4C63-BA5E-7391F203659A

1.tmp

NSLog(@"%@",NSTemporaryDirectory());  

输出结果:

/private/var/mobile/Applications/326640A7-6E27-4C63-BA5E-7391F203659A/tmp/

2.Myapp.app

NSLog(@"%@",[[NSBundle mainBundle] bundlePath]);

输出结果:

/var/mobile/Applications/326640A7-6E27-4C63-BA5E-7391F203659A/PhoneCall.app  

3.Documents

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  

NSString *path = [paths objectAtIndex:0];  

或者写成: NSString *path = [paths lastObject]; 

//documentDirectory只有一个元素所以取第一个元素根最后一个元素是一样的。 

NSLog(@"%@",path);  

输出结果:

/var/mobile/Applications/326640A7-6E27-4C63-BA5E-7391F203659A/Documents

:这里用到的NSSearchPathForDirectoriesInDomains方法需要解释下,其声明如下:

FOUNDATION_EXPORT NSArray *NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde);

该方法用于返回指定范围内的指定名称的目录的路径集合。有三个参数:

  • directory

NSSearchPathDirectory类型的enum值,表明我们要搜索的目录名称,比如这里用NSDocumentDirectory表明我们要搜索的是Documents目录。如果我们将其换成NSCachesDirectory就表示我们搜索的是Library/Caches目录。

  • domainMask

NSSearchPathDomainMask类型的enum值,指定搜索范围,这里的NSUserDomainMask表示搜索的范围限制于当前应用的沙盒目录。还可以写成NSLocalDomainMask(表示/Library)、NSNetworkDomainMask(表示/Network)等。

  • expandTilde

BOOL值,(YES: /User/userName. NO: ~)表示是否展开波浪线~。我们知道在iOS中~的全写形式是/User/userName,该值为YES即表示写成全写形式,为NO就表示直接写成“~” 。

三、NSFileManager的一些相关操作:

1)创建文件夹:

 NSString *myDirectory = [documentDirectory stringByAppendingPathComponent:@"test"];

     BOOL ok = [fileManage createDirectoryAtPath:myDirectory withIntermediateDirectories:YES attributes:nil error:&error];

2)取得一个目录下得所有文件名:(如上面的myDirectory)可用

NSArray *file = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];



NSArray *files = [fileManager subpathsAtPath: myDirectory ];



NSArray *files=[fileManager  contentsOfDirectoryAtPath:documentDirectory error:&error];



3)读取某个文件:

NSData *data = [fileManger contentsAtPath:myFilePath];//myFilePath是包含完整路径的文件名

或直接用NSData 的类方法:

NSData *data = [NSData dataWithContentOfPath:myFilePath];




4)保存某个文件:

可以用 NSFileManager的

- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;

或 NSData 的

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

- (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;



5)字符串写入文件:

[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

[NSString stringWithContentsOfFile...];



6)移动文件:

if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)



7)删除文件:

if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)



8)判断是否是文件夹:

BOOL isDir = NO;

[fileManager fileExistsAtPath:path isDirectory:(&isDir)];

 if (isDir) { ...}



9)以下代码用于获取本机上的文件资源或图片

获取文本:

NSFileManager *fileManager=[NSFileManager defaultManager];

NSData *data=[fileManager contentsAtPath:@"/Developer/Documentation/wxWidgets/docs/lgpl.txt"];

NSString *string=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@",string);

获取图片:

NSData *myData=[fileManager contentsAtPath:@"/Users/ruby/Desktop/Photo1.jpg"];

UIImage *myImage=[UIImage imageWithData:myData];


imageView.image=myImage;