iOS 沙盒

沙盒

沙盒机制

官网原文:

For security purposes, an iOS app’s interactions with the file system are limited to the directories inside the app’s sandbox directory. During installation of a new app, the installer creates a number of container directories for the app inside the sandbox directory. Each container directory has a specific role. The bundle container directory holds the app’s bundle, whereas the data container directory holds data for both the app and the user. The data container directory is further divided into a number of subdirectories that the app can use to sort and organize its data. The app may also request access to additional container directories—for example, the iCloud container—at runtime.
These container directories constitute the app’s primary view of the file system.

出于安全目的,iOS 应用程序与文件系统的交互仅限于该应用程序沙盒目录内的内容。在安装新应用程序期间,安装程序会在沙盒目录中为该应用程序创建多个容器目录。每个容器都有自己的功能。 bundle 容器目录保存应用程序的包,而数据容器目录保存应用程序和用户的数据。数据容器目录进一步分为多个子目录,应用程序可以使用这些子目录来排序和组织其数据。该应用程序还可能在运行时请求访问其他容器目录,例如 iCloud 容器。

沙盒结构

这些容器目录构成了应用程序的文件系统的主要视图。如图在这里插入图片描述
所有的非代码文件都会保存在沙盒中,例如属性文件plist、文本文件、图像、图标、媒体资源等。

名称作用路径
沙盒NSHomeDirectory()
MyApp.app是app的bundle文件,此目录包含app及其所有资源,开发者不能写入此目录。为防止篡改,bundle文件在安装时进行了签名,开发者只有读权限,没有写权限[[NSBundlemainBundle] bundlePath]
Documents/存储用户生成的内容,用户可见[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject]
Library/可创建子文件夹。可以用来放置希望被备份但不希望被用户看到的数据。该路径下的文件夹,除Caches以外,都会被iTunes备份[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES) lastObject]
Library/Caches缓存文件,日志文件最好也放在这个目录[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) firstObject]
Library/PreferencePanes保存应用程序的偏好设置文件(使用 NSUserDefaults类设置时创建,不应该手动创建。NSUserDefaults,plist文件都放在此处)[NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory,NSUserDomainMask,YES) firstObject]
tmp/保存应用运行时所需要的临时数据。iphone 重启时,会自动清除该目录下所有文件;例如录制视频完成后是放在/temp/;所以应该将视频文件从该文件夹转移到相册中,否则视频文件就会被系统清理掉NSTemporaryDirectory()

NSSearchPathDirectory这个枚举里还有很多沙盒文件路径,感兴趣可以去看看
iTunes在与iPhone同步时,备份所有的Documents和Library文件(除caches)

需要修改bundle中的文件

需求场景:在公司引入的第三方库Doraemonkit中需要进行文件修改,其在项目中已经成为一个bundle文件了,上面说,bundle只可读不可写
处理方案:初始化时判断该文件是否存在,存在即将其copy一份存储在Documents/文件夹中,之后的读写操作都使用Documents/文件夹中的文件。
代码示例:

// 读取本地JSON文件
- (NSDictionary *)readLocalFile {
    NSData *data;
    NSError *error;
    // 判断document文件是否存在,不存在就写入一份
    // document文件有写入权限
    NSString *writeFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"JsonFile.json"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeFilePath]) {
        // 从bundle中读取
        NSBundle *bundle = [NSBundle bundleForClass:NSClassFromString(@"DoraemonManager")];
        NSURL *url = [bundle URLForResource:@"DoraemonKit" withExtension:@"bundle"];
        if(!url) return [NSDictionary new];
        NSBundle *fileBundle = [NSBundle bundleWithURL:url];
        NSString *filePath = [fileBundle pathForResource:@"JsonFile" ofType:@"json"];
        // 数据写入document文件
        data = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
        [data writeToFile:writeFilePath atomically:YES];
    } else {
        data = [NSData dataWithContentsOfFile:writeFilePath options:NSDataReadingMappedIfSafe error:&error];
    }
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值