iOS程序都是在沙盒里面运行的,但是具体目录是咋样的呢?我写了个函数,list了所有的目录(包括文件)。咱们先来看一下:
- (void)fileList:(NSString*)directory{ printf(">>>:%s\n",[directory UTF8String]); NSError *error = nil; NSFileManager * fileManager = [NSFileManager defaultManager]; NSArray *fileList = [fileManager contentsOfDirectoryAtPath:directory error:&error]; for (NSString* each in fileList) { NSMutableString* path = [[NSMutableString alloc]initWithString:directory]; [path appendFormat:@"/%@",each]; [self fileList:path]; [path release]; } }
[self filelist:NSHomeDirectory()];
来看一下打印结果:
真机:
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/Directory
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/Info.plist
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/PkgInfo
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/ResourceRules.plist
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/_CodeSignature
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/_CodeSignature/CodeResources
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/embedded.mobileprovision
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/en.lproj
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/en.lproj/InfoPlist.strings
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/en.lproj/ViewController_iPad.nib
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Directory.app/en.lproj/ViewController_iPhone.nib
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Documents
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Library
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Library/Caches
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Library/Preferences
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Library/Preferences/.GlobalPreferences.plist
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/Library/Preferences/com.apple.PeoplePicker.plist
>>>:/var/mobile/Applications/A3209D39-4D02-40C2-99B4-4111A218CF1F/tmp
模拟器:
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Directory.app
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Directory.app/Directory
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Directory.app/en.lproj
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Directory.app/en.lproj/InfoPlist.strings
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Directory.app/en.lproj/ViewController_iPad.nib
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Directory.app/en.lproj/ViewController_iPhone.nib
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Directory.app/Info.plist
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Directory.app/PkgInfo
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Documents
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Library
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Library/Caches
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Library/Preferences
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Library/Preferences/.GlobalPreferences.plist
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/Library/Preferences/com.apple.PeoplePicker.plist
>>>:/Users/liuwei/Library/Application Support/iPhone Simulator/5.1/Applications/595FAC37-23AA-434A-84BE-3274C7A8A6F1/tmp
分析:
虽然真机和模拟器有点不一样,但是其实是大同小异。NSHomeDirectory() 是获取沙盒的根目录,然后就是他下面的其他目录:Directory.app(Directory是我这个工程的名字)、Documents、Library、tmp,Library下面又有一些子目录等等。
Directory.app 是程序包目录,主要包含和程序相关的文件,Documents你可以自己放一些文件到这个目录下,Library 也是个重要的目录,比如你的NSUserDefaults的数据就是在Libarary下面的Preferences中,还有 Caches 等等,tmp看名字就知道是一个存放临时文件的。我们主要用到的还是documentd和library。
不多说了,要想明白,自己把我的代码copy过去试一下就知道了。