天天看点

iOS--沙盒机制(sandbox)及相关操作简略了解

iOS和Android在资源储存上是有区别的,在Android平台,虽然说每一应用都会有自己独立的空间,这份独立的空间储存了应用的信息和数据库,但是在开发的时候会更多时候把一些下载资源或图片资源直接放在SD等外部储存中。但是来到了iOS平台就不是这样了,iOS平台中每一应用独享一份储存空间,这份独享的空间只能允许应用自身访问,其他应用不能进行访问,而且应用不能访问到其他应用的独享空间,这么一个空间就叫到沙盒(sandbox)。这只是我自己个人独立的见解,有所错误或不足望能提出,共同学习。

sandbox的目录结构:

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

Library:储存应用的默认设置和其他状态信息

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

tmp:提供一个即时创建临时文件的地方,重启时这个文件夹的内容将被清空

进行示例Test中模拟器沙盒文件夹中可看到如下图文件和文件夹:

iOS--沙盒机制(sandbox)及相关操作简略了解

在程序中需要使用这几个文件夹的话,获取相应文件夹的代码如下:

// 获得应用的Document文件夹位置路径
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"documentPath:%@", [documentPaths objectAtIndex:0]);
    // 获得应用的Library文件夹位置路径
    NSArray *libraryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSLog(@"libraryPath:%@", [libraryPaths objectAtIndex:0]);
    // 获得应用的Library/Caches文件夹位置路径
    NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSLog(@"cachePath:%@", [cachesPaths objectAtIndex:0]);
    // 获得应用的Tmp文件夹位置路径
    NSString *tmpPath = NSTemporaryDirectory();
    NSLog(@"tmpPath:%@", tmpPath);
           

把数据写入文件中,两个操作,第一个是把一个数组写入到Document文件夹的data.txt文件中,第二个是把一个字符串写入到Document文件夹的data1.txt文件中

// 把数据写入文件
    NSString *documentPath = [documentPaths objectAtIndex:0];
    // 需要写入的数据(数组)
    NSArray *dataArray = @[@"hello", @"world", @"你好"];
    // 写入文件路径
    NSString *document = [documentPath stringByAppendingPathComponent:@"data.txt"];
    // 写入
    [dataArray writeToFile:document atomically:YES];
    NSString *document1 = [documentPath stringByAppendingPathComponent:@"data1.txt"];
    // 写入(字符串)
    [@"helloworld" writeToFile:document1 atomically:YES encoding:NSUTF8StringEncoding error:nil];
           

运行结果就是可看到Document文件夹中多了两个文件(data.txt和data1.txt)

iOS--沙盒机制(sandbox)及相关操作简略了解

打开这两个文件,可看出,如果是储存纯数组的话,系统会把数组转换成xml格式的plist文件,而字符串则正常为字符串。

data.txt:

<?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>hello</string>
	<string>world</string>
	<string>你好</string>
</array>
</plist>
           

data1.txt:

helloworld
           

从文件中读取数据,一样两个操作,第一个是从文件“data.txt”中读取数组数据,第二个是从文件“data1.txt”中读取字符串数据

// 从文件读取数据
    NSString *documentPath = [documentPaths objectAtIndex:0];
    // 文件路径
    NSString *document = [documentPath stringByAppendingPathComponent:@"data.txt"];
    // 文件路径1
    NSString *document1 = [documentPath stringByAppendingPathComponent:@"data1.txt"];
    // 读取
    NSArray *dataFromFile = [[NSArray alloc] initWithContentsOfFile:document];
    NSString *dataFromFile1 = [[NSString alloc] initWithContentsOfFile:document1 encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"数组数据:%@", dataFromFile);
    NSLog(@"字符串数据:%@", dataFromFile1);
           

结果:

iOS--沙盒机制(sandbox)及相关操作简略了解

至此,结束~~

本文示例Demo放在Github:https://github.com/gaussli/SandboxTest