【精】iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)
1、沙盒机制介绍
iOS 中的沙盒机制(SandBox)是一种安全体系。
每一个 iOS 应用程序都有一个单独的文件系统(存储空间)。并且仅仅能在相应的文件系统中进行操作,此区域被称为沙盒。
全部的非代码文件都要保存在此,比如属性文件 plist、文本文件、图像、图标、媒体资源等。
2、沙盒文件夹结构
通常情况下,每一个沙盒包括下面文件夹及文件:
- /AppName.app 应用程序的程序包文件夹。因为应用程序必须经过签名,所以不能在执行时对这个文件夹中的内容进行改动。否则会导致应用程序无法启动。
- /Documents/ 保存应用程序的关键数据文件和用户数据文件等。
iTunes 同步时会备份该文件夹。
- /Library/Caches 保存应用程序使用时产生的支持文件和缓存文件,还有日志文件最好也放在这个文件夹。iTunes 同步时不会备份该文件夹。
- /Library/Preferences 保存应用程序的偏好设置文件(使用 NSUserDefaults 类设置时创建。不应该手动创建)。
- /tmp/ 保存应用执行时所须要的暂时数据,iphone 重新启动时,会清除该文件夹下全部文件。
文件夹结构例如以下图所看到的:
补充1:对于上述描写叙述能够这样举例理解,一个记事本应用,用户写的东西须要保存起来。这些东西是用户自行生成的。则须要放在 Documents 文件夹里。一个新闻应用,假设须要从server下载东西展示给用户看,下载的东西就放在 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应用软件沙盒sandbox相关知识(整理)
1.iOS沙盒机制原理 iOS应用程序只能在该程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...
随机推荐
- ZH奶酪:在博客中添加Latex公式
1. 点击编辑器中的插入图片: 2.在URL输入下边的地址: http://latex.codecogs.com/gif.latex?你的latex代码 就可以了-
- asp.net 使用JS获得串口数据
使用JS获得串口数据 JavaScript语言通常是一种网页编程语言,比较适合前台的一些应用设计.对于本地设备和数据的操作有比较大的限制.由于项目的需要,我需要获得本机的串口数据并显示在web端.我们 ...
- 网站中超链接方式直接添加QQ好友
使用情景: 在图中点击图片,会弹出添加qq好友的窗口进行好友添加. 链接如下: tencent://AddContact/?fromId=45&fromSubId=1&subcmd=a ...
- vb sendmessage 详解1
SendMessage函数的常用消息及其应用(有点长,希望能对大家有所帮助)函数原型: Declare Function SendMessage Lib "user32" Alia ...
- ios8.1.3Cydia重装
1.下载deb包 2.把包放到/var/mobile/Media/下 3.终端输入:dpkg -i /var/mobile/Media/*.deb 然后输入:su -c uicache mobile ...
- TOMCAT问题总结
迁移时间--2017年7月9日14:58:12Author:Marydon CreateTime--2016年12月25日21:55:09Author:MarydonTomcat问题总结问题一 A ...
- Android WiFi开发
概述 介绍Android WiFi的扫描.连接.信息.以及WiFi热点等等的实现,并用代码实现. 详细 代码下载:http://www.demodashi.com/demo/10660.html 一. ...
- openstack里面的Provider network 和 Tenant network 的区别
openstack里面的Provider network 和 Tenant network 的区别 openstack里面的网络相对复杂.经常有人对几个网络概念搞混淆,这里基本说明下 Openstac ...
- iOS 排序算法总结、二分法查找
1.插入排序 在要排序的一组数中,假设前面(n-1) [n>=2] 个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序的.如此反复循环,直到全部排好顺序. 直接插 ...
- wap站、手机APP 接入支付宝、微信、银联支付。
一.wap站 ①.支付宝接入 1.开发前准备:申请一个通过实名认证的企业支付宝账号,并申请开通手机WAP支付功能. 2.流程 参数准备: 企业支付宝账号的PID(也叫ParnerID)和KEY,如果使 ...