zoukankan      html  css  js  c++  java
  • iOS沙盒(sandbox)机制及获取沙盒路径

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

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

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

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

        (4)沙盒根目录里有四个文件夹:

               AppName.app目录:这是应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,

                                           所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动。

                   Documents目录:一般应该把应用程序的数据文件存到这个文件夹里,用于存储用户数据或其他应该定

                                          期备份的信息。

                         Library目录:此文件下有两个文件夹,Caches存储应用程序再次启动所需的信息;Preferences包

                                       含应用程序偏好设置文件,不过不要在这里修改偏好设置。

                           tmp目录:存放临时文件,即应用程序再次启动不需要的文件。

    二、获取沙盒路径

        (1)获取沙盒根目录的方法,有以下几种:

                1、用NSHomeDirectory获取              

        NSString *path = NSHomeDirectory();//主目录
        NSLog(@"NSHomeDirectory:%@ 
     ",path);
    

               2、用用户名获取

       NSString *userName = NSUserName();//获取该应用程序的用户名
       NSString *rootPath = NSHomeDirectoryForUser(userName);
       NSLog(@"NSHomeDirectoryForUser:%@ 
     ",rootPath);

        (2)获取Document路径

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];//Documents目录
        NSLog(@"NSDocumentDirectory:%@ 
     ",documentsDirectory);

        (3)获取tmp路径

       NSString *temPaths = NSTemporaryDirectory();//在里面写数据,程序退出后会没有
       NSLog(@"temPaths:%@ 
    ",temPaths);

       (4)获取cache路径

        NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *cachesDirectory = [cachesPaths objectAtIndex:0];
        NSLog(@"NSCachedDirectory:%@ 
     ",cachesDirectory);

       (5)获取应用程序程序包中资源文件路径的方法

         例如获取程序包中一个图片资源(apple.png)路径的方法:代码中的mainBundle类方法用于返回一个代表应用程序包的对象。

        NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
        UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

    三、写入文件

    1     //获取Document路径
    2     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    3     NSString *documentsDirectory=[paths objectAtIndex:0];//Documents目录
    4     NSLog(@"NSDocumentDirectory:%@ 
     ",documentsDirectory);
    5     
    6     //向documentDirectory写入文件
    7     NSArray *array = [[NSArray alloc] initWithObjects:@"hello",@"content",nil];
    8     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"test.txt"];
    9     [array writeToFile:filePath atomically:YES];

    四、读取文件

    1     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"test.txt"];
    2     NSArray *array_1 = [[NSArray alloc]initWithContentsOfFile:filePath];
    3     NSLog(@"读取Document中的文件:%@",array_1);
  • 相关阅读:
    02/Oct/2019:11:55:28 类型的时间转换为
    Flume 实时获取日志内容插入MySQL
    MySQL UTC时间转北京时间 | convert_tz()函数
    使用kettle实现循环
    python 获取系统环境变量 os.environ and os.putenv
    Python
    linux如何判断上一条命令执行是否正确
    局域网两台主机通过网络共享文件
    Kettle(Pentaho)实现web方式远程执行job或transformation
    kettle发送邮件 运行日志
  • 原文地址:https://www.cnblogs.com/FightingLuoYin/p/4429046.html
Copyright © 2011-2022 走看看