美文网首页
sandbox 内存管理

sandbox 内存管理

作者: 楚简约 | 来源:发表于2017-02-08 11:03 被阅读0次

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

    1、应用程序可以在自己的沙盒里运作,但是不能访问任何其他应用程序的沙盒
    2、应用程序间不能共享数据,沙盒里的文件不能被复制到其他应用程序文件夹中,也不能把其他应用程序文件夹中的文件复制到沙盒里。
    3、苹果禁止任何读、写沙盒以外的文件,禁止应用程序将内容写到沙盒以外的文件夹中。
    4、 iOS应用程序的沙盒中包括三个文件夹:

    Documents
    Apple官方建议将程序中建立的或程序浏览的文件数据都保存在Documents目录里,iTunes备份数据时或恢复数据时,会备份或恢复此目录;
    Library
    主要存储的是默认的设置或其它的状态信息。包含以下两个文件夹:
    Caches
    存储缓存文件,存储应用程序再次启动所需的,itunes不会备份该目录;
    Preferences
    存储应用程序偏好设置文件,一般不修改这里存放的文件;
    tmp
    提供的是一个存储临时文件的地方。iPhone一旦重启tmp目录下的内容就会被清空。

    二、如何获取沙盒路径
    1、获取沙盒根目录
    (1)、通过NSStringNSHomeDirectory(void)直接获取 NSString homePath =NSHomeDirectory(); (2)、通过NSStringNSHomeDirectoryForUser(NSStringuserName)获取
    NSString *userName =NSUserName();
    NSString *homePath2 = NSHomeDirectoryForUser(userName);

    2、获取沙盒其他文件路径 Documents、Library、Caches通过一下方法进行搜索: NSArray*NSSearchPathForDirectoriesInDomains(NSSearchPathDirectorydirectory, NSSearchPathDomainMaskdomainMask, BOOL expandTilde)方法:
    NSSearchPathDirectory,枚举值,制定搜索沙盒中的子目录
    NSSearchPathDomainMask, 枚举值,指定搜索范围
    expandTilde,是否显示全路径

    ①获取Documents路径
    NSArray *arrayOfDocPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *docPath = [arrayOfDocPath objectAtIndex:0];

    ②获取Library路径
    NSArray *arrayOfLibPath =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
    NSString *libPath = [arrayOfLibPath objectAtIndex:0];

    ③获取Caches路径
    NSArray *arrayOfCache =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
    NSString *cachesPath = [arrayOfCache objectAtIndex:0];

    ④获取Preferences路径
    NSString *prePath = [libPathstringByAppendingPathComponent:@"Preferences"];
    //通过Library路径拼接上文件夹名,一般不会读取、修改该文件夹下的文件

    ⑤获取tmp路径
    NSString *temPath =NSTemporaryDirectory();

    ⑥获取app目录
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];

    ⑦获取app中资源文件路径
    NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"01loading.png" ofType:nil];
    //该方法还有多个其他形式

    相关文章

      网友评论

          本文标题:sandbox 内存管理

          本文链接:https://www.haomeiwen.com/subject/rfyvittx.html