NSString *path = NSHomeDirectory();
通过该代码得到的是应用程序目录的路径,在该目录下有三个文件夹:Documents、Library、temp和一个.app的包
该目录下就是应用程序的沙盒,应用程序只能访问该目录下的文件夹
例子:
一、App Home(/AppBane,aoo)。包含App bundle的目录,不要在该路径下写任何文件。
1、
NSString *path1 = NSHomeDirectory();
NSLog(@"path1:%@", path1);
path1:/Users/joyworks/Library/Application Support/iPhone Simulator/7.1/Applications/D6301C3F-91E3-453E-9709-F2D04114FB9D
二、/Documents/。使用该路径放置关键数据,也就是不能通过App重新生成的数据。该路径可通过配置实现iTunes共享文件。可被iTunes备份。(现在保存在该路径下的文件还需要考虑iCloud同步)
2、
NSString *path2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"path2:%@", path2);
结果:path2:/Users/joyworks/Library/Application Support/iPhone Simulator/7.1/Applications/D6301C3F-91E3-453E-9709-F2D04114FB9D/Documents
NSString *path3 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"path3:%@", path3);
结果:path3:/Users/joyworks/Library/Application Support/iPhone Simulator/7.1/Applications/D6301C3F-91E3-453E-9709-F2D04114FB9D/Documents
三、/Library/。该路径下一般保存着用户配置文件。可创建子文件夹。可以用来放置您希望被备份但不希望被用户看到的数据。该路径下的文件夹,除Caches以外,都会被iTunes备份。
4、
NSString *path4 = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
NSLog(@"path4:%@", path4);
结果:path4:/Users/joyworks/Library/Application Support/iPhone Simulator/7.1/Applications/D6301C3F-91E3-453E-9709-F2D04114FB9D/Library
四、/tmp/。使用该路径保存临时文件。App应该删除那些不再被使用的文件,系统也会在App关闭后删除残留文件。该路径下的文件不会被iTunes备份。
5、
NSString *path5 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"path5:%@", path5);
结果:path5:/Users/joyworks/Library/Application Support/iPhone Simulator/7.1/Applications/D6301C3F-91E3-453E-9709-F2D04114FB9D/Library/Caches
6、
NSString *path6 = [NSHomeDirectory() stringByAppendingPathComponent:@"temp"];
NSLog(@"path6:%@", path6);
结果:path6:/Users/joyworks/Library/Application Support/iPhone Simulator/7.1/Applications/D6301C3F-91E3-453E-9709-F2D04114FB9D/temp