• 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);
  • 相关阅读:
    HTTP传递数据的几种方法
    Python LOGGING使用方法
    EntityFramework Code First 添加唯一键
    The model backing the <Database> context has changed since the database was created.
    No connection string named '***' could be found in the application config file
    Generating a new ASP.NET session in the current HTTPContext
    add .json handler support in IIS 7
    Reset Entity-Framework Migrations
    Changing the type of a property with EF Code First
    命令行模式下 MYSQL导入导出.sql文件的方法
  • 原文地址:https://www.cnblogs.com/qq744890760/p/4046718.html
Copyright © 2020-2023  润新知