Sandbox沙盒

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sandbox沙盒相关的知识,希望对你有一定的参考价值。

一、Sandbox沙盒特点:

         1、应用程序之间相互隔离;

         2、应用程序只能访问自己本地的内容,不能访问其他app文件目录结构(越狱手机除外);

         3、为应用程序构建一个完整的运行空间;

         4、应用程序有自己的缓存和数据备份。

 

二、沙盒目录结构:

          1、Documnets;

           2、HellWord.app

           3、Library;

           4、Tmp;

三、app所在沙盒Sandbox目录:

       模拟器:

            ~/Library/Application Support/iPhone Simulator/6.0/Applications/8F08C873-76C7-406F-AEAD-B499137787DD

         真机:

           /var/mobile/Applications/8F08C873-76C7-406F-AEAD-B499137787DD

四、获取App Home目录

      获取Home目录:

                 NSString *homeDirectory = NSHomeDirectory();

          结果类似:

                ~/Library/Application Support/iPhone Simulator/6.0/Applications/8F08C873-76C7-406F-AEAD-B499137787DD

五、HelloWorld.app 目录

                NSString *appPath =[ [NSBundle MainBundle] BundlePath];

             结果类似:

              ~/Library/Application Support/iPhone Simulator/6.0/Applications/8F08C873-76C7-406F-AEAD-B499137787DD/HelloWorld.app

六、获取Documents目录

            NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoey,NSUserDomainMask,YES);

            NSString *path = [paths objectAtIndex:0];

            结果类似:

                ~/Library/Application Support/iPhone Simulator/6.0/Applications/8F08C873-76C7-406F-AEAD-B499137787DD/Documents

七、获取Library 目录

            NSArray *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);

            NSString *path = [paths objectAtIndex:0];

           结果类似:

             ~/Library/Application Support/iPhone Simulator/6.0/Applications/8F08C873-76C7-406F-AEAD-B499137787DD/Library

八、获取Caches目录

            NSArray *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);           

           NSString *path = [paths objectAtIndex:0];

           结果类似:

九、获取Tmp目录

            

             NSString *tmpDir = NSTemporaryDirectory();

           结果类似:

              ~/Library/Application Support/iPhone Simulator/6.0/Applications/8F08C873-76C7-406F-AEAD-B499137787DD/Tmp

十、获取应用程序程序包中资源文件路径的方法:
例如获取程序包中一个图片资源(apple.png)路径的方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

  //1)第一种情况,文件图标是黄色的,文件夹是假的,都会拷贝到HelloWorld.app 中
    //而且文件名称不能重名
    NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"1" ofType:@".jpg"];
    NSLog(@"image is %@",imagePath);
    
    NSString *imagePath2 = [[NSBundle mainBundle ]pathForResource:@"2.jpg" ofType:nil];
    NSLog(@"image2 is %@",imagePath2);
    
    //Users/L-ios/Library/Developer/CoreSimulator/Devices/F5988D84-CB68-4DEC-805E-414D905304BE/data/Containers/Bundle/Application/528B83A0-F896-4759-8B57-D4EE2DCDBBE6/各种目录获取.app/image2/1.jpg
    //2)第二种情况,文件夹图标是绿色的,这个文件真实存在,内容拷贝到HelloWorld.app中,
    NSString *image2Path = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"jpg" inDirectory:@"image2"];
    NSLog(@"image2path is %@",image2Path);
    
    NSString *image2Path2 = [[NSBundle mainBundle]pathForResource:@"2.jpg" ofType:nil inDirectory:@"image2"];
    NSLog(@"image2Path2 is %@ ",image2Path2);

以上是关于Sandbox沙盒的主要内容,如果未能解决你的问题,请参考以下文章

关于App Sandbox

iOS 沙盒(sandbox)结构 使用 实例

sandbox是啥

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

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

理解 iOS沙盒(sandbox)机制