一、模拟器沙盒目录
文件都在我的用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录实际上是Library。
由于应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件:
Documents:应用中用户数据能够放在这里,iTunes备份和恢复的时候会包括此目录
tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
iTunes在与iPhone同步时,备份全部的Documents和Library文件。
iPhone在重启时,会丢弃全部的tmp文件。
查看方法:
方法一、能够设置显示隐藏文件,而后在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/文件夹。这里面就是模拟器的各个程序的沙盒目录了。
方法二、这种方法更方便,在Finder上点->前往->前往文件夹,输入/Users/username/Library/Application Support/iPhone Simulator/ 前往。
username这里写用户名。 ios
代码查看目录:缓存
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);
结果以下:app
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; }