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

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

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

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

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

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

      Documents:一般应该把应用程序的数据文件存到这个文件夹里,用于存储用户数据或其他应该定期备份的信息。

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

      Temp:存放临时文件,即应用再次启动不需要的文件。

    二、获取沙盒路径

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

      a、用NSHomeDirectory获取

    1 NSString *path = NSHomeDirectory();
    2 NSLog(@"path = %@",path);

      b、用用户名获取

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

      c、获取Documents路径

    1 NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    2 NSString *filePath = [documentPath stringByAppendingString:@"data.plist"];//向Documents里添加文件
    3 NSLog(@"filePath = %@",filePath);

      d、获取temp路径

    1 NSString *tempPath = NSTemporaryDirectory();// 获取temp目录,如果在里面写数据,当程序退出会清空
    2 NSLog(@"tempPath = %@",tempPath);

      e、获取caches路径

    1 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    2 NSString *cachesPath = [paths objectAtIndex:0];
    3 NSLog(@"cachesPath = %@",cachesPath);
  • 相关阅读:
    SSH、SCP和SFTP 解析(转)
    SQL Server数据库partition by 与ROW_NUMBER()函数使用详解 (转载)
    Git版本控制与工作流详解(转)
    IQueryable,IEnumberable,.AsEnumerable() 和 .AsQueryable() (转载)
    ASP.NET 中Http处理流程与 HttpModule,HttpHandler学习之初步认知
    xml Node 是否存在
    MVC-前台调用后台action 传递upload file 参数问题
    ResXResourceWriter 与ResourceWriter
    "= ="与 equals 的区别 摘录
    jpg文件格式分析
  • 原文地址:https://www.cnblogs.com/qq744890760/p/4046718.html
Copyright © 2011-2022 走看看