【iOS开发-3】sandbox沙盒介绍以及如何取得沙盒里面各文件的路径,图片导入方式和图片路径获取

其实每个APP的程序都是单独的一个文件夹,每个APP之间相互之间独立,所以它们的文件夹也是独立的,这个文件夹就是沙盒。沙盒好处,安全。坏处,程序之间彼此共享数据较为困难。

(1)如何找到应用程序的沙盒源文件?

一般沙盒源文件都存放在~/Library/Application Support/iPhone Simulator/版本号/Applications/的文件夹中,但是这个Library(中文名叫资源库)文件夹是隐藏的。所以需要方法才能点击进去:在Finder中按CMD+SHIFT+G,然后再前往的框中输入~/Library,也就是直接前往,而不是通过点击前往。

当然也是可以用终端一直cd进去,注意有的目录名有两个单词中间空格前面要加 \ 转义,如cd ./Library/Application\ Support/iPhone\ Simulator/

后面选择模拟的版本,然后进入Application,里面都是应用程序,当然是用哈希码命名,里面有4个文件:

Documents、Library、tmp和hello.app,存放永久和临时数据的,这就是每个沙盒里面的主要文件。


(2)如何用代码获得沙盒里面的文件路径?

注明:NSBundle就是获取.app文件里面资源的一种方式。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //获取整个程序所在目录
    NSString *homePath=NSHomeDirectory();
    NSLog(@"%@",homePath);
    //获取.app文件目录
    NSString *appPath=[[NSBundle mainBundle]bundlePath];
    NSLog(@"%@",appPath);
    //Documents目录
    NSArray *arr1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@",[arr1 objectAtIndex:0]);
    //Library目录
    NSArray *arr2=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSLog(@"%@",[arr2 objectAtIndex:0]);
    //Caches目录,在Library下面
    NSArray *arr3=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSLog(@"%@",[arr3 objectAtIndex:0]);
    //tmp目录
    NSString *tmpPath=NSTemporaryDirectory();
    NSLog(@"%@",tmpPath);
    //用整个程序目录加上tmp就拼凑出tmp目录,其他目录都可这样完成
    NSString *tmpPath_1=[homePath stringByAppendingPathComponent:@"tmp"];
    NSLog(@"%@",tmpPath_1);
    // Override point for customization after application launch.
    return YES;
}
结果:

2014-09-23 21:05:58.966 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D
2014-09-23 21:05:58.966 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Bundle/Application/E7D6885C-9CC1-461D-867C-8D0DA5AF183A/hello.app
2014-09-23 21:05:58.967 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D/Documents
2014-09-23 21:05:58.967 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D/Library
2014-09-23 21:05:58.967 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D/Library/Caches
2014-09-23 21:05:58.967 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D/tmp/
2014-09-23 21:05:58.967 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D/tmp

(3)导入图片的方式以及如何获取路径

我们往程序中导入一个img文件夹或者一张图片时有两种方式:Creat groups和Creat folder reference,前者表示导入进来的图片都在.app(它其实是哥文件夹)中,列示在左侧的img文件夹只是用来分组而不是真正的目录;而后者表示不仅把图片导入进来,还把img目录导入进来,即.app下有个img文件夹,里面存放的时图片。



我们看到如下获取图片路径都用到NSBundle,其实因为都是在.app文件夹中,而NSBundle就是获取.app里资源的,所以在强化记忆一遍。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //Create groups方式,图片直接在.app里,以下两种方式相同
    NSString *path1=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"png"];
    NSString *path2=[[NSBundle mainBundle]pathForResource:@"1.png" ofType:nil];
    NSLog(@"%@,%@",path1,path2);
    
    //Create folder reference方式,图片在img里,以下三种方式相同
    NSString *path3=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"png" inDirectory:@"img"];
    NSString *path4=[[NSBundle mainBundle]pathForResource:@"1.png" ofType:nil inDirectory:@"img"];
    NSString *path5=[[NSBundle mainBundle]pathForResource:@"img/1.png" ofType:nil inDirectory:nil];
    NSLog(@"%@,%@,%@",path3,path4,path5);
    // Override point for customization after application launch.
    return YES;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。