版权声明:本文为博主原创文章,转载请声明出处: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 沙盒路径,可通过在项目中执行下述代码打印获取:

  1. NSString *homeDirectoryPath = NSHomeDirectory();

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

  1. // 获取沙盒根目录路径
  2. NSString *homeDirectoryPath = NSHomeDirectory();
  3. // 获取 Applications 路径
  4. NSString *appDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  5. // 获取 Documents 路径
  6. NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  7. // 获取 Caches 路径
  8. NSString *cachesDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  9. // 获取 tmp 路径
  10. NSString *tmpDirectoryPath = NSTemporaryDirectory();

4、NSFileManager

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

  1. [NSFileManager defaultManager]

5、程序包(NSBundle)

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

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

  1. // 获取应用程序的 main bundle
  2. NSBundle *mainBundle = NSBundle.mainBundle;
  3. // 使用 main bundle 获取资源路径
  4. NSString *filePath = [mainBundle pathForResource:@"logo" ofType:@"png"];

6、工具类

FileUtil.h

  1. #import <UIKit/UIKit.h>
  2. @interface FileUtil : NSObject
  3. /**
  4. *  获取 home 路径
  5. *
  6. *  @return
  7. */
  8. + (NSString *)homeDirectoryPath;
  9. /**
  10. *  获取 app 路径
  11. *
  12. *  @return
  13. */
  14. + (NSString *)appDirectoryPath;
  15. /**
  16. *  获取 document 路径
  17. *
  18. *  @return
  19. */
  20. + (NSString *)documentDirectoryPath;
  21. /**
  22. *  获取 caches 路径
  23. *
  24. *  @return
  25. */
  26. + (NSString *)cachesDirectoryPath;
  27. /**
  28. *  获取 tmp 路径
  29. *
  30. *  @return
  31. */
  32. + (NSString *)tmpDirectoryPath;
  33. /**
  34. *  判断目录是否存在
  35. *
  36. *  @param directoryPath 目录路径
  37. *
  38. *  @return
  39. */
  40. + (BOOL)directoryExist:(NSString *)directoryPath;
  41. /**
  42. *  判断文件是否存在
  43. *
  44. *  @param filePath 文件路径
  45. *
  46. *  @return
  47. */
  48. + (BOOL)fileExist:(NSString *)filePath;
  49. /**
  50. *  在父目录下创建子目录
  51. *
  52. *  @param parentDirectoryPath 父目录路径
  53. *  @param directoryName       子目录名称
  54. *
  55. *  @return
  56. */
  57. + (BOOL)createDirectoryAtParentDirectory:(NSString *)parentDirectoryPath directoryName:(NSString *)directoryName;
  58. /**
  59. *  在父目录下创建子文件
  60. *
  61. *  @param parentDirectoryPath 父目录路径
  62. *  @param fileName            子文件名称
  63. *
  64. *  @return
  65. */
  66. + (BOOL)createFileAtParentDirectory:(NSString *)parentDirectoryPath fileName:(NSString *)fileName;
  67. /**
  68. *  删除目录
  69. *
  70. *  @param directoryPath 目录路径
  71. *
  72. *  @return
  73. */
  74. + (BOOL)deleteDirectoryAtPath:(NSString *)directoryPath;
  75. /**
  76. *  删除文件
  77. *
  78. *  @param filePath 文件路径
  79. *
  80. *  @return
  81. */
  82. + (BOOL)deleteFileAtPath:(NSString *)filePath;
  83. /**
  84. *  获取父目录下的子内容(包含目录和文件)
  85. *
  86. *  @param parentDirectoryPath 父目录路径
  87. *
  88. *  @return
  89. */
  90. + (NSArray *)contentsAtParentDirectory:(NSString *)parentDirectoryPath;
  91. /**
  92. *  获取父目录下的所有子目录名称
  93. *
  94. *  @param parentDirectoryPath 父目录路径
  95. *
  96. *  @return
  97. */
  98. + (NSArray *)directoryNamesAtParentDirectory:(NSString *)parentDirectoryPath;
  99. /**
  100. *  获取父目录下的所有子目录路径
  101. *
  102. *  @param parentDirectoryPath 父目录路径
  103. *
  104. *  @return
  105. */
  106. + (NSArray *)directoryPathsAtParentDirectory:(NSString *)parentDirectoryPath;
  107. /**
  108. *  获取父目录下的所有子文件名称
  109. *
  110. *  @param parentDirectoryPath 父目录路径
  111. *
  112. *  @return
  113. */
  114. + (NSArray *)fileNamesAtParentDirectoryPath:(NSString *)parentDirectoryPath;
  115. /**
  116. *  获取父目录下的所有子文件路径
  117. *
  118. *  @param parentDirectoryPath 父目录路径
  119. *
  120. *  @return
  121. */
  122. + (NSArray *)filePathsAtParentDirectoryPath:(NSString *)parentDirectoryPath;
  123. @end

FileUtil.m

  1. #import "FileUtil.h"
  2. @interface FileUtil ()
  3. @end
  4. @implementation FileUtil
  5. + (NSString *)homeDirectoryPath
  6. {
  7. return NSHomeDirectory();
  8. }
  9. + (NSString *)appDirectoryPath
  10. {
  11. NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
  12. return [array objectAtIndex:0];
  13. }
  14. + (NSString *)documentDirectoryPath
  15. {
  16. NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  17. return [array objectAtIndex:0];
  18. }
  19. + (NSString *)cachesDirectoryPath
  20. {
  21. NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  22. return [array objectAtIndex:0];
  23. }
  24. + (NSString *)tmpDirectoryPath
  25. {
  26. return NSTemporaryDirectory();
  27. }
  28. + (BOOL)directoryExist:(NSString *)directoryPath
  29. {
  30. NSFileManager *fileManager = [NSFileManager defaultManager];
  31. BOOL isDirectory = NO;
  32. BOOL exist = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
  33. if (isDirectory && exist) {
  34. return YES;
  35. }
  36. return NO;
  37. }
  38. + (BOOL)fileExist:(NSString *)filePath
  39. {
  40. NSFileManager *fileManager = [NSFileManager defaultManager];
  41. return [fileManager fileExistsAtPath:filePath];
  42. }
  43. + (BOOL)createDirectoryAtParentDirectory:(NSString *)parentDirectoryPath directoryName:(NSString *)directoryName
  44. {
  45. NSFileManager *fileManager = [NSFileManager defaultManager];
  46. NSString *directoryPath = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, directoryName];
  47. return [fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
  48. }
  49. + (BOOL)createFileAtParentDirectory:(NSString *)parentDirectoryPath fileName:(NSString *)fileName
  50. {
  51. NSFileManager *fileManager = [NSFileManager defaultManager];
  52. NSString *filePath = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, fileName];
  53. return [fileManager createFileAtPath:filePath contents:nil attributes:nil];
  54. }
  55. + (BOOL)deleteDirectoryAtPath:(NSString *)directoryPath
  56. {
  57. NSFileManager *fileManager = [NSFileManager defaultManager];
  58. if ([self directoryExist:directoryPath]) {
  59. return [fileManager removeItemAtPath:directoryPath error:nil];
  60. }
  61. return NO;
  62. }
  63. + (BOOL)deleteFileAtPath:(NSString *)filePath
  64. {
  65. NSFileManager *fileManager = [NSFileManager defaultManager];
  66. if ([self fileExist:filePath]) {
  67. return [fileManager removeItemAtPath:filePath error:nil];
  68. }
  69. return NO;
  70. }
  71. + (NSArray *)contentsAtParentDirectory:(NSString *)parentDirectoryPath
  72. {
  73. NSFileManager *fileManager = [NSFileManager defaultManager];
  74. return [fileManager contentsOfDirectoryAtPath:parentDirectoryPath error:nil];
  75. }
  76. + (NSArray *)directoryNamesAtParentDirectory:(NSString *)parentDirectoryPath
  77. {
  78. NSFileManager *fileManager = [NSFileManager defaultManager];
  79. BOOL isDirectory = NO;
  80. NSMutableArray *directoryPaths = [[NSMutableArray alloc] init];
  81. for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {
  82. NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];
  83. if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {
  84. if (isDirectory) {
  85. [directoryPaths addObject:content];
  86. }
  87. }
  88. }
  89. return [directoryPaths copy];
  90. }
  91. + (NSArray *)directoryPathsAtParentDirectory:(NSString *)parentDirectoryPath
  92. {
  93. NSFileManager *fileManager = [NSFileManager defaultManager];
  94. BOOL isDirectory = NO;
  95. NSMutableArray *directoryPaths = [[NSMutableArray alloc] init];
  96. for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {
  97. NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];
  98. if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {
  99. if (isDirectory) {
  100. [directoryPaths addObject:path];
  101. }
  102. }
  103. }
  104. return [directoryPaths copy];
  105. }
  106. + (NSArray *)fileNamesAtParentDirectoryPath:(NSString *)parentDirectoryPath
  107. {
  108. NSFileManager *fileManager = [NSFileManager defaultManager];
  109. BOOL isDirectory = NO;
  110. NSMutableArray *filePaths = [[NSMutableArray alloc] init];
  111. for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {
  112. NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];
  113. if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {
  114. if (!isDirectory) {
  115. [filePaths addObject:content];
  116. }
  117. }
  118. }
  119. return [filePaths copy];
  120. }
  121. + (NSArray *)filePathsAtParentDirectoryPath:(NSString *)parentDirectoryPath
  122. {
  123. NSFileManager *fileManager = [NSFileManager defaultManager];
  124. BOOL isDirectory = NO;
  125. NSMutableArray *filePaths = [[NSMutableArray alloc] init];
  126. for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {
  127. NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];
  128. if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {
  129. if (!isDirectory) {
  130. [filePaths addObject:path];
  131. }
  132. }
  133. }
  134. return [filePaths copy];
  135. }
  136. @end

7、结语

参考资料如下:

About Files and Directories

NSFileManager Class Reference

NSBundle Class Reference

 
 

iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)的更多相关文章

  1. 沙盒SandBox

    每个App都有自己的沙盒,也就是一个存储空间.App之间没有权限访问对方的沙盒资源.沙盒的目录下有三个文件夹:Documents.Library.temp 目录结构 Documents:用于存储用户数 ...

  2. IOS 学习之 iOS沙盒(sandbox) 介绍 沙盒机制 文件操作(一)

    1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...

  3. iOS 沙盒(sandbox)机制和文件操作

    本文参看了 http://www.uml.org.cn/mobiledev/201209211.asp#1 这篇文章中的介绍,尊重原著. 1.IOS沙盒机制 IOS应用程序只能在本应用程序中创建的文件 ...

  4. IOS学习之IOS沙盒(sandbox)机制和文件操作

    IOS学习之IOS沙盒(sandbox)机制和文件操作(一) 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都 ...

  5. iOS学习之iOS沙盒(sandbox)机制和文件操作(一)

    1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...

  6. iOS学习之iOS沙盒(sandbox)机制和文件操作复习

    1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...

  7. iOS学习之iOS沙盒(sandbox)机制和文件操作1

    iOS学习之iOS沙盒(sandbox)机制和文件操作 接上篇 iOS学习之iOS沙盒(sandbox)机制和文件操作(一) 我们看看如何获取应用程序沙盒目录.包括真机的沙盒的目录. 1.获取程序的H ...

  8. IOS沙盒(sandbox)机制和文件操作

    IOS学习之IOS沙盒(sandbox)机制和文件操作   作者:totogo2010 ,发布于2012-9-21,来源:CSDN   目录: IOS学习之IOS沙盒(sandbox)机制和文件操作( ...

  9. 查看iOS沙盒(SanBox)文件

    转载:http://www.2cto.com/kf/201211/169212.html 每一个iOS程序都一个自己的文件系统,这个文件系统叫应用程序沙盒(SanBox),它存放这代码以外的文件,其他 ...

  10. iOS开发之--沙盒的操作

    iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...

随机推荐

  1. POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)

    题目链接:http://poj.org/problem?id=2752 题意:给你一个字符串,求出所有前缀后缀(既是前缀又是后缀的子串)的长度 思路:首先整个字符串肯定既是前缀又是后缀,为最大的前缀后 ...

  2. BZOJ 1034: [ZJOI2008]泡泡堂BNB( 贪心 )

    贪心...用最弱的赢最弱的,用最强的赢最强的,否则用最弱的和最强的比... (贴个官方题解:将双方的选手均按从强到弱排序,然后第一次扫描尽可能用当前剩下的选手中能赢对手当前最强选手中最弱的一个去赢得胜 ...

  3. BZOJ 2337: [HNOI2011]XOR和路径( 高斯消元 )

    一位一位考虑异或结果, f(x)表示x->n异或值为1的概率, 列出式子然后高斯消元就行了 --------------------------------------------------- ...

  4. SqlHelp

    using System.Configuration;using System.Data; public class SqlHelp { private static string connectio ...

  5. hihocoder #1260 : String Problem I

    题目链接   时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 我们有一个字符串集合S,其中有N个两两不同的字符串. 还有M个询问,每个询问给出一个字符串w,求有多少S中的 ...

  6. A Byte of Python 笔记(5)函数:定义、形参、局部变量、默认参数、关键参数

    第7章  函数 函数是重要的程序段.它们允许你给一块语句一个名称,然后你可以在程序的任何地方使用这个名称任意多次地运行这个语句块.这被称为 调用 函数. 定义函数 函数通过 def 关键字定义.def ...

  7. Python一些字符串判断和转换

    设s是字符串: s.isalnum()      判断所有字符都是数字或者字母 s.isalpha()  判断所有字符都是字母 s.isdigit()  判断所有字符都是数字 s.islower() ...

  8. JavaScript中的鼠标滚轮事件详解

    JavaScript中的鼠标滚轮事件详解/*Firefox注册事件*/ ~~~Firefox: addEventListener('DOMMouseScroll', handler, false)if ...

  9. 使用 RMI + ZooKeeper 实现远程调用框架

    目录[-] 1 发布 RMI 服务1.1 定义一个 RMI 接口1.2 编写 RMI 接口的实现类1.3 通过 JNDI 发布 RMI 服务2 调用 RMI 服务3 RMI 服务的局限性4 使用 Zo ...

  10. android天气查询(一)websevice之ksoap2软件包的使用

    对于用到天气信息,首先我想: 第一:数据不可能是我测得的,必须是网上的信息. 第二:网上的信息分为好多种,具体哪种比较好一点,这里我总结了两种. 第三:数据JSON怎么解析. 第四:如何提出数据与显示 ...