【精】iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)

1、沙盒机制介绍

iOS 中的沙盒机制(SandBox)是一种安全体系。
每个 iOS 应用程序都有一个单独的文件系统(存储空间),而且只能在对应的文件系统中进行操作,此区域被称为沙盒。所有的非代码文件都要保存在此,例如属性文件 plist、文本文件、图像、图标、媒体资源等。

2、沙盒目录结构

通常情况下,每个沙盒包含以下目录及文件:

  • /AppName.app 应用程序的程序包目录。由于应用程序必须经过签名,所以不能在运行时对这个目录中的内容进行修改,否则会导致应用程序无法启动。
  • /Documents/ 保存应用程序的重要数据文件和用户数据文件等。iTunes 同步时会备份该目录。
  • /Library/Caches 保存应用程序使用时产生的支持文件和缓存文件,还有日志文件最好也放在这个目录。iTunes 同步时不会备份该目录。
  • /Library/Preferences 保存应用程序的偏好设置文件(使用 NSUserDefaults 类设置时创建,不应该手动创建)。
  • /tmp/ 保存应用运行时所需要的临时数据,iphone 重启时,会清除该目录下所有文件。

目录结构如下图所示:


补充1:对于上述描述可以这样举例理解,一个记事本应用,用户写的东西需要保存起来,这些东西是用户自行生成的,则需要放在 Documents 目录里。一个新闻应用,如果需要从服务器下载东西展示给用户看,下载的东西就放在 Library/Caches 目录里。苹果审核对这个要求很严格,主要原因是 iCloud 的同步问题。

补充2:如果想知道真机或者模拟器 App 沙盒路径,可通过在项目中执行下述代码打印获取:

NSString *homeDirectoryPath = NSHomeDirectory();

3、获取沙盒中各目录路径

// 获取沙盒根目录路径
NSString *homeDirectoryPath = NSHomeDirectory();

// 获取 Applications 路径
NSString *appDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES) objectAtIndex:0];

// 获取 Documents 路径
NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

// 获取 Caches 路径
NSString *cachesDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

// 获取 tmp 路径
NSString *tmpDirectoryPath = NSTemporaryDirectory();

4、NSFileManager

使用 FileManager 可以对沙盒中的目录、文件进行操作。通过如下方式可以获取 NSFileManager 的单例:

[NSFileManager defaultManager]

5、程序包(NSBundle)

iOS 应用都是通过 bundle 进行封装的,可以狭隘地将 bundle 理解为上述沙盒中的 AppName.app 文件。在 Finder 中,会把 bundle 当做一个文件显示从而防止用户误操作导致程序文件损坏,但其实内部是一个目录,包含了图像、媒体资源、编译好的代码、nib 文件等,这个目录称为 main bundle。

Cocaoa 提供了 NSBundle 类封装了 bundle 操作。

// 获取应用程序的 main bundle
NSBundle *mainBundle = NSBundle.mainBundle;

// 使用 main bundle 获取资源路径 
NSString *filePath = [mainBundle pathForResource:@"logo" ofType:@"png"];

6、工具类

FileUtil.h
#import <UIKit/UIKit.h>

@interface FileUtil : NSObject

/**
 *  获取 home 路径
 *
 *  @return
 */
+ (NSString *)homeDirectoryPath;

/**
 *  获取 app 路径
 *
 *  @return
 */
+ (NSString *)appDirectoryPath;

/**
 *  获取 document 路径
 *
 *  @return
 */
+ (NSString *)documentDirectoryPath;

/**
 *  获取 caches 路径
 *
 *  @return
 */
+ (NSString *)cachesDirectoryPath;

/**
 *  获取 tmp 路径
 *
 *  @return
 */
+ (NSString *)tmpDirectoryPath;

/**
 *  判断目录是否存在
 *
 *  @param directoryPath 目录路径
 *
 *  @return
 */
+ (BOOL)directoryExist:(NSString *)directoryPath;

/**
 *  判断文件是否存在
 *
 *  @param filePath 文件路径
 *
 *  @return
 */
+ (BOOL)fileExist:(NSString *)filePath;

/**
 *  在父目录下创建子目录
 *
 *  @param parentDirectoryPath 父目录路径
 *  @param directoryName       子目录名称
 *
 *  @return
 */
+ (BOOL)createDirectoryAtParentDirectory:(NSString *)parentDirectoryPath directoryName:(NSString *)directoryName;

/**
 *  在父目录下创建子文件
 *
 *  @param parentDirectoryPath 父目录路径
 *  @param fileName            子文件名称
 *
 *  @return
 */
+ (BOOL)createFileAtParentDirectory:(NSString *)parentDirectoryPath fileName:(NSString *)fileName;

/**
 *  删除目录
 *
 *  @param directoryPath 目录路径
 *
 *  @return
 */
+ (BOOL)deleteDirectoryAtPath:(NSString *)directoryPath;

/**
 *  删除文件
 *
 *  @param filePath 文件路径
 *
 *  @return
 */
+ (BOOL)deleteFileAtPath:(NSString *)filePath;

/**
 *  获取父目录下的子内容(包含目录和文件)
 *
 *  @param parentDirectoryPath 父目录路径
 *
 *  @return
 */
+ (NSArray *)contentsAtParentDirectory:(NSString *)parentDirectoryPath;

/**
 *  获取父目录下的所有子目录名称
 *
 *  @param parentDirectoryPath 父目录路径
 *
 *  @return
 */
+ (NSArray *)directoryNamesAtParentDirectory:(NSString *)parentDirectoryPath;

/**
 *  获取父目录下的所有子目录路径
 *
 *  @param parentDirectoryPath 父目录路径
 *
 *  @return
 */
+ (NSArray *)directoryPathsAtParentDirectory:(NSString *)parentDirectoryPath;

/**
 *  获取父目录下的所有子文件名称
 *
 *  @param parentDirectoryPath 父目录路径
 *
 *  @return
 */
+ (NSArray *)fileNamesAtParentDirectoryPath:(NSString *)parentDirectoryPath;

/**
 *  获取父目录下的所有子文件路径
 *
 *  @param parentDirectoryPath 父目录路径
 *
 *  @return
 */
+ (NSArray *)filePathsAtParentDirectoryPath:(NSString *)parentDirectoryPath;

@end
FileUtil.m
#import "FileUtil.h"

@interface FileUtil ()

@end

@implementation FileUtil

+ (NSString *)homeDirectoryPath
{
    return NSHomeDirectory();
}

+ (NSString *)appDirectoryPath
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
    return [array objectAtIndex:0];
}

+ (NSString *)documentDirectoryPath
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [array objectAtIndex:0];
}

+ (NSString *)cachesDirectoryPath
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    return [array objectAtIndex:0];
}

+ (NSString *)tmpDirectoryPath
{
    return NSTemporaryDirectory();
}

+ (BOOL)directoryExist:(NSString *)directoryPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDirectory = NO;
    BOOL exist = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
    
    if (isDirectory && exist) {
        return YES;
    }
    return NO;
}

+ (BOOL)fileExist:(NSString *)filePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    return [fileManager fileExistsAtPath:filePath];
}

+ (BOOL)createDirectoryAtParentDirectory:(NSString *)parentDirectoryPath directoryName:(NSString *)directoryName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *directoryPath = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, directoryName];
    return [fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
}

+ (BOOL)createFileAtParentDirectory:(NSString *)parentDirectoryPath fileName:(NSString *)fileName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, fileName];
    return [fileManager createFileAtPath:filePath contents:nil attributes:nil];
}

+ (BOOL)deleteDirectoryAtPath:(NSString *)directoryPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([self directoryExist:directoryPath]) {
        return [fileManager removeItemAtPath:directoryPath error:nil];
    }
    return NO;
}

+ (BOOL)deleteFileAtPath:(NSString *)filePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([self fileExist:filePath]) {
        return [fileManager removeItemAtPath:filePath error:nil];
    }
    return NO;
}

+ (NSArray *)contentsAtParentDirectory:(NSString *)parentDirectoryPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    return [fileManager contentsOfDirectoryAtPath:parentDirectoryPath error:nil];
}

+ (NSArray *)directoryNamesAtParentDirectory:(NSString *)parentDirectoryPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDirectory = NO;
    NSMutableArray *directoryPaths = [[NSMutableArray alloc] init];
    for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {
        NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];
        if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {
            if (isDirectory) {
                [directoryPaths addObject:content];
            }
        }
    }
    return [directoryPaths copy];
}

+ (NSArray *)directoryPathsAtParentDirectory:(NSString *)parentDirectoryPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDirectory = NO;
    NSMutableArray *directoryPaths = [[NSMutableArray alloc] init];
    for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {
        NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];
        if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {
            if (isDirectory) {
                [directoryPaths addObject:path];
            }
        }
    }
    return [directoryPaths copy];
}

+ (NSArray *)fileNamesAtParentDirectoryPath:(NSString *)parentDirectoryPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDirectory = NO;
    NSMutableArray *filePaths = [[NSMutableArray alloc] init];
    for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {
        NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];
        if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {
            if (!isDirectory) {
                [filePaths addObject:content];
            }
        }
    }
    return [filePaths copy];
}

+ (NSArray *)filePathsAtParentDirectoryPath:(NSString *)parentDirectoryPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDirectory = NO;
    NSMutableArray *filePaths = [[NSMutableArray alloc] init];
    for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {
        NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];
        if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {
            if (!isDirectory) {
                [filePaths addObject:path];
            }
        }
    }
    return [filePaths copy];
}

@end

7、结语

参考资料如下:

About Files and Directories

NSFileManager Class Reference

NSBundle Class Reference

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统中所演示的数据均系杜撰,并非真实数据,包括单位名称、人名、地名和通信方式。 sandbox是一个基于django框架开发的轻量级办公平台,主要模块有:权限控制、资产(库存)管理、设备管理、客户信息管理和工单流程管理,其目的在于建立一套规范化、统一化和清晰化的标准服务流程,能够清晰的处理、记录和追踪服务流程,同时依赖于工单流程模块,建立技术服务文档共享库,让技术人员相互协作,分享经验,提升服务技术水平。 这套系统的开发主要是为了规范当前公司服务流程,改变服务混乱,项目多人多次服务无交接的问题,同时将公司资产(库存)和销售的设备一起做了集中管理和服务跟踪。 目前公司主要是对外销售安全产品和安全服务,作为一个小型乙方公司,上一套OA系统太过繁重,所以就自己动手来做了这么一套轻量级办公系统。 线上环境部署 系统运行环境 centos7 python3.6 mysql 5.6, 系统需要安装 docker , docker-compose 1、下载项目文件到你的系统 $ git clone https://github.com/RobbieHan/gistandard.git 2、进入项目目录,拷贝和修改配置文件 $ cd gistandard $ mkdir -p /sandbox/nginx $ cp config/nginx.conf /sandbox/nginx/ $ cp -r media /sandbox $ vim /sandbox/nginx/nginx.conf # 修改nginx配置文件中 server标签下的 server_name 为你系统的地址IP 或域名(sandbox安装完成通过这个地址来访问) 3、运行容器 $ docker-compose up -d 等等等等..... 查看容器运行状态: $ docker-compose ps Name Command State Ports -------------------------------------------------------------------------------- gistandard_mysql_1 docker-entrypoint.sh mysql ... Up 3306/tcp gistandard_web_1 bash -c uwsgi config/sandb ... Up 0.0.0.0:80->80/tcp 4、导入数据库文件 $ docker cp db_tools/data/basic_data.sql gistandard_mysql_1:/tmp $ docker-compose exec mysql bash $ mysql -uddadmin -p1234@abcd.com gistandard < /tmp/basic_data.sql 5、访问系统: http://your_ip 初始用户 admin 密码 !qaz@wsx 本地环境运行 如果只是想查看代码和系统功能,可以在本地windows环境下临时运行项目,或者使用开发工具运行项目 1、从github上下载项目文件到本地,或者使用git克隆项目 2、在windows上安装python3.6环境(也可以使用python虚拟环境,python环境的安装方法网上很多,这里不再写了) 3、修改gistandard/gistandard/settings.py文件中数据库连接配置DATABASES, 本地运行使用db.sqlite3数据库,内含测试数据 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } 4、安装依赖包,打开CMD命令行,进入项目主目录运行下面命令安装依赖包: 注意: 本地环境使用的是sqlite3数据库,安装依赖包前可以先将requirements文件中的mysqlclient那一行删掉,这个包是用来连接mysql数据库使用的,通常windows下安装这个包会报错,解决办法我在知乎境部署的文档中都有写过。 我windows下使用的是python虚拟环境,所以我下面CMD命令行开头是(gistandard)这个是我虚拟环境的名称,虚拟环境的使用方法也可以网上找下,或者关注我的知乎专栏,环境部署

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值