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

本文参看了 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的目录(绝对路径)
    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的目录,然后将这个文件写入到沙箱中的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 混合数据的读写  请参看原文最后面的内容。


大概就是这么多了吧!


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 上,应用的沙盒目录是私有的,其他应用无法直接访问。因此,要从 JS 中获取 iOS 沙盒内的文件,需要通过 Native Bridge 的方式,即将 JS 中的请求传递给原生代码,由原生代码来实现文件读取并返回结果给 JS。 以下是一个简单的示例,假设你要获取沙盒内的一个名为 `example.txt` 的文件: 1. 在原生代码中实现文件读取逻辑,例如使用 `NSFileManager` 类读取文件内容: ```objc - (NSString *)readFileContentsAtPath:(NSString *)path { NSFileManager *fileManager = [NSFileManager defaultManager]; NSData *data = [fileManager contentsAtPath:path]; return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; } ``` 2. 在 JS 中定义一个函数,用于调用原生代码并获取文件内容: ```js function getExampleFileContents() { // 调用 Native Bridge 方法,传递路径参数 const path = 'path/to/example.txt'; const contents = NativeBridge.readFileContentsAtPath(path); return contents; } ``` 3. 在原生代码中实现 Native Bridge 方法,并将其暴露给 JS 环境: ```objc - (void)registerNativeBridgeMethods { [self.bridge registerNativeMethod:@"readFileContentsAtPath" handler:^(NSString *path) { NSString *contents = [self readFileContentsAtPath:path]; // 将结果返回给 JS 环境 [self.bridge callHandler:@"onFileContentsReceived" withArguments:@[contents]]; }]; } ``` 4. 在 JS 中注册 `onFileContentsReceived` 回调函数,用于接收原生代码返回的文件内容: ```js function onFileContentsReceived(contents) { // 处理文件内容 console.log(contents); } NativeBridge.registerEventHandler('onFileContentsReceived', onFileContentsReceived); ``` 注意:以上示例仅为演示 Native Bridge 的基本用法,具体实现方式可能因框架、平台、业务需求等因素而异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值