iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)
版权声明:本文为博主原创文章,转载请声明出处:http://blog.csdn.net/jinnchang
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、结语
参考资料如下:
iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)的更多相关文章
- 沙盒SandBox
每个App都有自己的沙盒,也就是一个存储空间.App之间没有权限访问对方的沙盒资源.沙盒的目录下有三个文件夹:Documents.Library.temp 目录结构 Documents:用于存储用户数 ...
- IOS 学习之 iOS沙盒(sandbox) 介绍 沙盒机制 文件操作(一)
1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...
- iOS 沙盒(sandbox)机制和文件操作
本文参看了 http://www.uml.org.cn/mobiledev/201209211.asp#1 这篇文章中的介绍,尊重原著. 1.IOS沙盒机制 IOS应用程序只能在本应用程序中创建的文件 ...
- IOS学习之IOS沙盒(sandbox)机制和文件操作
IOS学习之IOS沙盒(sandbox)机制和文件操作(一) 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都 ...
- iOS学习之iOS沙盒(sandbox)机制和文件操作(一)
1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...
- iOS学习之iOS沙盒(sandbox)机制和文件操作复习
1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...
- iOS学习之iOS沙盒(sandbox)机制和文件操作1
iOS学习之iOS沙盒(sandbox)机制和文件操作 接上篇 iOS学习之iOS沙盒(sandbox)机制和文件操作(一) 我们看看如何获取应用程序沙盒目录.包括真机的沙盒的目录. 1.获取程序的H ...
- IOS沙盒(sandbox)机制和文件操作
IOS学习之IOS沙盒(sandbox)机制和文件操作 作者:totogo2010 ,发布于2012-9-21,来源:CSDN 目录: IOS学习之IOS沙盒(sandbox)机制和文件操作( ...
- 查看iOS沙盒(SanBox)文件
转载:http://www.2cto.com/kf/201211/169212.html 每一个iOS程序都一个自己的文件系统,这个文件系统叫应用程序沙盒(SanBox),它存放这代码以外的文件,其他 ...
- iOS开发之--沙盒的操作
iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...
随机推荐
- hibernate -inverse
one to many inverse=false只能设置维护关联关系的多的一方, inverse属性: 默认为false,表示本方维护关联关系. 如果为true,表示本方不维护关联关系(并不意味着对 ...
- Maven Spring JUnit 在Maven Clean Install时报
问题: Maven Clean Install时, 遇到报错package org.junit does not exist 明显, Unit Test在Compile阶段就被检查了. 而POM.xm ...
- 采用translate实现垂直水平居中
今天分享一个利用css3新特性实现垂直水平居中的方法. 通过对元素进行绝对定位再配合transform中的translate实现. 代码如下: html <div id="conten ...
- PHP的一些函数
//进制转换类 base_convert //字符转十六进制 binhex
- C和指针 读书笔记
准备复习一下之前读过的<C和指针>,主要看之前标记过的地方. 感觉像第一次看的地方再记录一下-- 1.预处理器读入源代码,根据预处理指令对其进行修改,然后将修改后的源代码交给编译器. 2. ...
- Qt的Model/View Framework解析(数据是从真正的“肉(raw)”里取得,Model提供肉,所以读写文件、操作数据库、网络通讯等一系列与数据打交道的工作就在model中做了)
最近在看Qt的Model/View Framework,在网上搜了搜,好像中文的除了几篇翻译没有什么有价值的文章.E文的除了Qt的官方介绍,其它文章也很少.看到一个老外在blog中写道Model/Vi ...
- php浮点数计算比较及取整不准确解决方法
原文:php浮点数计算比较及取整不准确解决方法 php有意思的现象,应该是很多编程语言都会有这样的现象.这个是因为计算机的本身对浮点数识别的问题..... $f = 0.58; var_dump(in ...
- 启用Apache Mod_rewrite模块
Ubuntu 环境 在终端中执行 sudo a2enmod rewrite 指令后,即启用了 Mod_rewrite 模块. 另外,也可以通过将 /etc/apache2/mods-available ...
- HDU 1147 Pick-up sticks
题解:每放一根棍子,都判断一下它与它前面的且在顶端的棍子是否相交,相交的话则将相应的棍子从解空间中除去. #include <cstdio> const double eps=1e-14; ...
- [uva 11762]Race to 1[概率DP]
引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...