zoukankan      html  css  js  c++  java
  • iOS沙盒路径的查看和使用

    1、模拟器沙盒目录

    文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library。

     

    因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件:

    Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录

    tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除

    Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除

     

    iTunes在与iPhone同步时,备份所有的Documents和Library文件。

    iPhone在重启时,会丢弃所有的tmp文件。

     

     

    查看方法:

    方法1、可以设置显示隐藏文件,然后在Finder下直接打开。设置查看隐藏文件的方法如下:打开终端,输入命名

    (1)显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true

    (2)隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false

    (3)输完单击Enter键,退出终端,重新启动Finder就可以了 重启Finder:鼠标单击窗口左上角的苹果标志-->强制退出-->Finder-->

    现在能看到资源库文件夹了。 

    打开资源库后找到/Application Support/iPhone Simulator/文件夹。这里面就是模拟器的各个程序的沙盒目录了。

    方法2、这种方法更方便,在Finder上点->前往->前往文件夹,输入/Users/username/Library/Application Support/iPhone Simulator/  前往。

    username这里写用户名。 

     

    代码查看目录:

     

        NSString *path = NSHomeDirectory();//主目录

        NSLog(@"NSHomeDirectory:%@",path);

        NSString *userName = NSUserName();//与上面相同

        NSString *rootPath = NSHomeDirectoryForUser(userName);

        NSLog(@"NSHomeDirectoryForUser:%@",rootPath);

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsDirectory=[paths objectAtIndex:0];//Documents目录

        NSLog(@"NSDocumentDirectory:%@",documentsDirectory);

     

    结果如下:

    2013-09-03 20:31:27.210 ios那啥[8383:c07] NSHomeDirectory:/Users/wmm/Library/Application Support/iPhone Simulator/6.1/Applications/D803DBD2-9CB2-4D18-9152-6E9398EFF5DB

    2013-09-03 20:31:27.210 ios那啥[8383:c07] NSHomeDirectoryForUser:/Users/wmm/Library/Application Support/iPhone Simulator/6.1/Applications/D803DBD2-9CB2-4D18-9152-6E9398EFF5DB

    2013-09-03 20:31:27.211 ios那啥[8383:c07] NSDocumentDirectory:/Users/wmm/Library/Application Support/iPhone Simulator/6.1/Applications/D803DBD2-9CB2-4D18-9152-6E9398EFF5DB/Documents

     

     

    自定义类返回各目录路径:

     

    #import <Foundation/Foundation.h>

     

    @interface ICSandboxHelper : NSObject

     

    + (NSString *)homePath;     // 程序主目录,可见子目录(3个):Documents、Library、tmp

    + (NSString *)appPath;        // 程序目录,不能存任何东西

    + (NSString *)docPath;        // 文档目录,需要ITUNES同步备份的数据存这里,可存放用户数据

    + (NSString *)libPrefPath;    // 配置目录,配置文件存这里

    + (NSString *)libCachePath;    // 缓存目录,系统永远不会删除这里的文件,ITUNES会删除

    + (NSString *)tmpPath;        // 临时缓存目录,APP退出后,系统可能会删除这里的内容

    + (BOOL)hasLive:(NSString *)path; //判断目录是否存在,不存在则创建

     

     

    #import "ICSandboxHelper.h"

     

    @implementation ICSandboxHelper

     

    + (NSString *)homePath{

        return NSHomeDirectory();

    }

     

    + (NSString *)appPath

    {

        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);

        return [paths objectAtIndex:0];

    }

     

    + (NSString *)docPath

    {

        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        return [paths objectAtIndex:0];

    }

     

    + (NSString *)libPrefPath

    {

        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

        return [[paths objectAtIndex:0] stringByAppendingFormat:@"/Preference"];

    }

     

    + (NSString *)libCachePath

    {

        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

        return [[paths objectAtIndex:0] stringByAppendingFormat:@"/Caches"];

    }

     

    + (NSString *)tmpPath

    {return [NSHomeDirectory() stringByAppendingFormat:@"/tmp"];

    }

     

    + (BOOL)hasLive:(NSString *)path

    {

        if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:path] )

        {

            return [[NSFileManager defaultManager] createDirectoryAtPath:path

                                             withIntermediateDirectories:YES

                                                              attributes:nil

                                                                   error:NULL];

        }

        

        return NO;

    }

  • 相关阅读:
    CodeForces gym Nasta Rabbara lct
    bzoj 4025 二分图 lct
    CodeForces 785E Anton and Permutation
    bzoj 3669 魔法森林
    模板汇总——快读 fread
    bzoj2049 Cave 洞穴勘测 lct
    bzoj 2002 弹飞绵羊 lct裸题
    HDU 6394 Tree 分块 || lct
    HDU 6364 Ringland
    nyoj221_Tree_subsequent_traversal
  • 原文地址:https://www.cnblogs.com/h-tao/p/4969081.html
Copyright © 2011-2022 走看看