zoukankan      html  css  js  c++  java
  • 沙盒机制(sandBox)

    一、每个iOS应用SDK都被限制在“沙盒”中,“沙盒”相当于一个加了仅主人可见权限的文件夹,苹果对沙盒有以下几条限制。

        (1)、应用程序可以在自己的沙盒里运作,但是不能访问任何其他应用程序的沙盒。

        (2)、应用程序间不能共享数据(通过网络共享),沙盒里的文件不能被复制到其他应用程序文件夹中,也不能把其他应用程序文件夹中的文件复制到沙盒里。

        (3)、苹果禁止任何读、写沙盒以外的文件,禁止应用程序将内容写到沙盒以外的文件夹中。

    二、沙盒目录(根目录):/Users/****/Library/Application Support/iPhone Simulator/7.0/Applications/5F4F79F4-D1D0-4D48-8F3F-0647AA6DA930

    根目录下共有4个目录,分别:

    Doucuments:苹果建议将程序中创建的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录

    Library:Library下有两个文件夹,Caches存储应用程序再次启动所需的信息,Preferences包含应用程序偏好设置文件,不过不要在这里修改偏好设置。

    tmp:创建和存放临时文件的地方,此目录下文件在应用退出删除

    *****.app:存放图片、文本的二进制文件,通过[[NSBundle mainBundle] bundlePath];可以访问该目录

    三、xcode里黄色的文件夹都是虚的,蓝色都才是实的。

     

       //获取用户的根目录
        NSString *homePath=NSHomeDirectory();
        NSLog(@"homePath=%@",homePath);
        
        //获得document路径(通过拼接可以得到根目录下面的四个文件夹)
        NSString *docPath=[homePath stringByAppendingPathComponent:@"Documents"];
        NSLog(@"docPath=%@",docPath);
        
        
        //不通过拼接直接获得
        NSString *docPath1=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
        NSLog(@"docPath1=%@",docPath1);
        
        //获取library文件夹
        NSString *libraryPath=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)objectAtIndex:0];
        NSLog(@"libraryPath=%@",libraryPath);
        
        //获得chches目录(13是NSCachesDirectory枚举值,在枚举中数值和名称一样的使用)
        NSString *cachesPath=[NSSearchPathForDirectoriesInDomains(13, NSUserDomainMask, YES)objectAtIndex:0];
        NSLog(@"cachesPath=%@",cachesPath);
        
        
        //获取临时目录
        NSString *tmpPath=NSTemporaryDirectory();
        NSLog(@"tmpPath=%@",tmpPath);
        
        
        
    //======================在沙盒路径里面创建文件=========================
        
      //1、找到想要创建文件路径,
        NSString *helloPath=[docPath1 stringByAppendingPathComponent:@"hello.txt"];
      //2、创建一个文件管理者对象,来管理我们的文件(文件管理者是一个单例对象)
        NSFileManager *fm=[NSFileManager defaultManager];
      //3、判断刚才创建的文件路径下面是否已经存一个文件可以管理,如果存在就不用创建了,如果不存在就创建一个
        if (![fm fileExistsAtPath:helloPath])//不存在
        {
            [fm createFileAtPath:helloPath contents:nil attributes:nil];
        }
        else//存在
        {
            NSLog(@"文件已经存在,不用再次创建");
        }
        
        //文件写入内容
        NSString *str=@"iPhone";
        [str writeToFile:helloPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        
        NSData *data=[NSData dataWithContentsOfFile:helloPath];
        NSLog(@"data=%@",data);
        
        NSString *str1=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"str1=%@",str1);
        
        
        //NSError *erroe=nil;
    //文件的删除
        /*
        if ([fm fileExistsAtPath:helloPath])
        {
            [fm removeItemAtPath:helloPath error:&erroe];
            NSLog(@"error=%@",[erroe localizedDescription]);//错误描述,一般如果会出错,该方法会告你错误的原因
        }
        */
        NSString * helloPath1=[libraryPath stringByAppendingPathComponent:@"hello.txt"];
        
    //文件的移动
        if ([fm fileExistsAtPath:helloPath])
        {
            [fm moveItemAtPath:helloPath toPath:helloPath1 error:nil];//要写文件的最终路径,不能写文件夹的路径
        }
        else
        {
            NSLog(@"没找到想要移动的文件");
        }
    //查看一路径下有多少文件(隐藏文件也可查看)
        NSArray *array=[fm contentsOfDirectoryAtPath:docPath1 error:nil];
        NSLog(@"array=%@",array);
        
        
        //查找从外部推入到***.app目录的文件,用NSBundle来查找路径
        NSString *iPadPath=[[NSBundle mainBundle] pathForResource:@"iPad" ofType:@"txt" inDirectory:nil];
        NSLog(@"iPadpath=%@",iPadPath);
        
        NSString *iPadStr=[NSString stringWithContentsOfFile:iPadPath encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"iPadStr=%@",iPadStr);

     

     

  • 相关阅读:
    TreeList 树形控件 实现带三种状态的CheckBox
    SQL 左外连接,右外连接,全连接,内连接(转)
    在DataTable中进行数据查询 (转)
    uva10594 Data Flow最小费用流,两个代码区别不大(我没看出区别),为什么一个对,另一个超时!!
    SGU142 Keyword好题
    uva 10881
    南京理工1747(数论)WA了好多遍!
    CF161D 树形dp
    uva 11646(大水题几何分类)
    求a加到b二进制加法有多少次进位。
  • 原文地址:https://www.cnblogs.com/huen/p/3536917.html
Copyright © 2011-2022 走看看