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 沙盒路径,可通过在项目中执行下述代码打印获取:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. NSString *homeDirectoryPath = NSHomeDirectory();  

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

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 获取沙盒根目录路径  
  2. NSString *homeDirectoryPath = NSHomeDirectory();  
  3.   
  4. // 获取 Applications 路径  
  5. NSString *appDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
  6.   
  7. // 获取 Documents 路径  
  8. NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
  9.   
  10. // 获取 Caches 路径  
  11. NSString *cachesDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
  12.   
  13. // 获取 tmp 路径  
  14. NSString *tmpDirectoryPath = NSTemporaryDirectory();  

4、NSFileManager

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

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [NSFileManager defaultManager]  

5、程序包(NSBundle)

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

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

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 获取应用程序的 main bundle  
  2. NSBundle *mainBundle = NSBundle.mainBundle;  
  3.   
  4. // 使用 main bundle 获取资源路径   
  5. NSString *filePath = [mainBundle pathForResource:@"logo" ofType:@"png"];  

6、工具类

FileUtil.h
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface FileUtil : NSObject  
  4.   
  5. /** 
  6.  *  获取 home 路径 
  7.  * 
  8.  *  @return 
  9.  */  
  10. + (NSString *)homeDirectoryPath;  
  11.   
  12. /** 
  13.  *  获取 app 路径 
  14.  * 
  15.  *  @return 
  16.  */  
  17. + (NSString *)appDirectoryPath;  
  18.   
  19. /** 
  20.  *  获取 document 路径 
  21.  * 
  22.  *  @return 
  23.  */  
  24. + (NSString *)documentDirectoryPath;  
  25.   
  26. /** 
  27.  *  获取 caches 路径 
  28.  * 
  29.  *  @return 
  30.  */  
  31. + (NSString *)cachesDirectoryPath;  
  32.   
  33. /** 
  34.  *  获取 tmp 路径 
  35.  * 
  36.  *  @return 
  37.  */  
  38. + (NSString *)tmpDirectoryPath;  
  39.   
  40. /** 
  41.  *  判断目录是否存在 
  42.  * 
  43.  *  @param directoryPath 目录路径 
  44.  * 
  45.  *  @return 
  46.  */  
  47. + (BOOL)directoryExist:(NSString *)directoryPath;  
  48.   
  49. /** 
  50.  *  判断文件是否存在 
  51.  * 
  52.  *  @param filePath 文件路径 
  53.  * 
  54.  *  @return 
  55.  */  
  56. + (BOOL)fileExist:(NSString *)filePath;  
  57.   
  58. /** 
  59.  *  在父目录下创建子目录 
  60.  * 
  61.  *  @param parentDirectoryPath 父目录路径 
  62.  *  @param directoryName       子目录名称 
  63.  * 
  64.  *  @return 
  65.  */  
  66. + (BOOL)createDirectoryAtParentDirectory:(NSString *)parentDirectoryPath directoryName:(NSString *)directoryName;  
  67.   
  68. /** 
  69.  *  在父目录下创建子文件 
  70.  * 
  71.  *  @param parentDirectoryPath 父目录路径 
  72.  *  @param fileName            子文件名称 
  73.  * 
  74.  *  @return 
  75.  */  
  76. + (BOOL)createFileAtParentDirectory:(NSString *)parentDirectoryPath fileName:(NSString *)fileName;  
  77.   
  78. /** 
  79.  *  删除目录 
  80.  * 
  81.  *  @param directoryPath 目录路径 
  82.  * 
  83.  *  @return 
  84.  */  
  85. + (BOOL)deleteDirectoryAtPath:(NSString *)directoryPath;  
  86.   
  87. /** 
  88.  *  删除文件 
  89.  * 
  90.  *  @param filePath 文件路径 
  91.  * 
  92.  *  @return 
  93.  */  
  94. + (BOOL)deleteFileAtPath:(NSString *)filePath;  
  95.   
  96. /** 
  97.  *  获取父目录下的子内容(包含目录和文件) 
  98.  * 
  99.  *  @param parentDirectoryPath 父目录路径 
  100.  * 
  101.  *  @return 
  102.  */  
  103. + (NSArray *)contentsAtParentDirectory:(NSString *)parentDirectoryPath;  
  104.   
  105. /** 
  106.  *  获取父目录下的所有子目录名称 
  107.  * 
  108.  *  @param parentDirectoryPath 父目录路径 
  109.  * 
  110.  *  @return 
  111.  */  
  112. + (NSArray *)directoryNamesAtParentDirectory:(NSString *)parentDirectoryPath;  
  113.   
  114. /** 
  115.  *  获取父目录下的所有子目录路径 
  116.  * 
  117.  *  @param parentDirectoryPath 父目录路径 
  118.  * 
  119.  *  @return 
  120.  */  
  121. + (NSArray *)directoryPathsAtParentDirectory:(NSString *)parentDirectoryPath;  
  122.   
  123. /** 
  124.  *  获取父目录下的所有子文件名称 
  125.  * 
  126.  *  @param parentDirectoryPath 父目录路径 
  127.  * 
  128.  *  @return 
  129.  */  
  130. + (NSArray *)fileNamesAtParentDirectoryPath:(NSString *)parentDirectoryPath;  
  131.   
  132. /** 
  133.  *  获取父目录下的所有子文件路径 
  134.  * 
  135.  *  @param parentDirectoryPath 父目录路径 
  136.  * 
  137.  *  @return 
  138.  */  
  139. + (NSArray *)filePathsAtParentDirectoryPath:(NSString *)parentDirectoryPath;  
  140.   
  141. @end  
FileUtil.m
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #import "FileUtil.h"  
  2.   
  3. @interface FileUtil ()  
  4.   
  5. @end  
  6.   
  7. @implementation FileUtil  
  8.   
  9. + (NSString *)homeDirectoryPath  
  10. {  
  11.     return NSHomeDirectory();  
  12. }  
  13.   
  14. + (NSString *)appDirectoryPath  
  15. {  
  16.     NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);  
  17.     return [array objectAtIndex:0];  
  18. }  
  19.   
  20. + (NSString *)documentDirectoryPath  
  21. {  
  22.     NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  23.     return [array objectAtIndex:0];  
  24. }  
  25.   
  26. + (NSString *)cachesDirectoryPath  
  27. {  
  28.     NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  29.     return [array objectAtIndex:0];  
  30. }  
  31.   
  32. + (NSString *)tmpDirectoryPath  
  33. {  
  34.     return NSTemporaryDirectory();  
  35. }  
  36.   
  37. + (BOOL)directoryExist:(NSString *)directoryPath  
  38. {  
  39.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  40.     BOOL isDirectory = NO;  
  41.     BOOL exist = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDirectory];  
  42.       
  43.     if (isDirectory && exist) {  
  44.         return YES;  
  45.     }  
  46.     return NO;  
  47. }  
  48.   
  49. + (BOOL)fileExist:(NSString *)filePath  
  50. {  
  51.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  52.     return [fileManager fileExistsAtPath:filePath];  
  53. }  
  54.   
  55. + (BOOL)createDirectoryAtParentDirectory:(NSString *)parentDirectoryPath directoryName:(NSString *)directoryName  
  56. {  
  57.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  58.     NSString *directoryPath = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, directoryName];  
  59.     return [fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];  
  60. }  
  61.   
  62. + (BOOL)createFileAtParentDirectory:(NSString *)parentDirectoryPath fileName:(NSString *)fileName  
  63. {  
  64.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  65.     NSString *filePath = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, fileName];  
  66.     return [fileManager createFileAtPath:filePath contents:nil attributes:nil];  
  67. }  
  68.   
  69. + (BOOL)deleteDirectoryAtPath:(NSString *)directoryPath  
  70. {  
  71.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  72.     if ([self directoryExist:directoryPath]) {  
  73.         return [fileManager removeItemAtPath:directoryPath error:nil];  
  74.     }  
  75.     return NO;  
  76. }  
  77.   
  78. + (BOOL)deleteFileAtPath:(NSString *)filePath  
  79. {  
  80.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  81.     if ([self fileExist:filePath]) {  
  82.         return [fileManager removeItemAtPath:filePath error:nil];  
  83.     }  
  84.     return NO;  
  85. }  
  86.   
  87. + (NSArray *)contentsAtParentDirectory:(NSString *)parentDirectoryPath  
  88. {  
  89.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  90.     return [fileManager contentsOfDirectoryAtPath:parentDirectoryPath error:nil];  
  91. }  
  92.   
  93. + (NSArray *)directoryNamesAtParentDirectory:(NSString *)parentDirectoryPath  
  94. {  
  95.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  96.     BOOL isDirectory = NO;  
  97.     NSMutableArray *directoryPaths = [[NSMutableArray alloc] init];  
  98.     for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {  
  99.         NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];  
  100.         if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {  
  101.             if (isDirectory) {  
  102.                 [directoryPaths addObject:content];  
  103.             }  
  104.         }  
  105.     }  
  106.     return [directoryPaths copy];  
  107. }  
  108.   
  109. + (NSArray *)directoryPathsAtParentDirectory:(NSString *)parentDirectoryPath  
  110. {  
  111.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  112.     BOOL isDirectory = NO;  
  113.     NSMutableArray *directoryPaths = [[NSMutableArray alloc] init];  
  114.     for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {  
  115.         NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];  
  116.         if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {  
  117.             if (isDirectory) {  
  118.                 [directoryPaths addObject:path];  
  119.             }  
  120.         }  
  121.     }  
  122.     return [directoryPaths copy];  
  123. }  
  124.   
  125. + (NSArray *)fileNamesAtParentDirectoryPath:(NSString *)parentDirectoryPath  
  126. {  
  127.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  128.     BOOL isDirectory = NO;  
  129.     NSMutableArray *filePaths = [[NSMutableArray alloc] init];  
  130.     for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {  
  131.         NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];  
  132.         if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {  
  133.             if (!isDirectory) {  
  134.                 [filePaths addObject:content];  
  135.             }  
  136.         }  
  137.     }  
  138.     return [filePaths copy];  
  139. }  
  140.   
  141. + (NSArray *)filePathsAtParentDirectoryPath:(NSString *)parentDirectoryPath  
  142. {  
  143.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  144.     BOOL isDirectory = NO;  
  145.     NSMutableArray *filePaths = [[NSMutableArray alloc] init];  
  146.     for (NSString *content in [self contentsAtParentDirectory:parentDirectoryPath]) {  
  147.         NSString *path = [NSString stringWithFormat:@"%@/%@", parentDirectoryPath, content];  
  148.         if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory]) {  
  149.             if (!isDirectory) {  
  150.                 [filePaths addObject:path];  
  151.             }  
  152.         }  
  153.     }  
  154.     return [filePaths copy];  
  155. }  
  156.   
  157. @end  

7、结语

参考资料如下:

About Files and Directories

NSFileManager Class Reference

NSBundle Class Reference

 
 
原文地址:https://www.cnblogs.com/wangyang1213/p/5435132.html