最近项目需要应用中集成了个cocoahttpserver服务,开发中一直用ios6真机调试的,但兼容ios7真机时就不行了,结果发现是把web的html等其他文件放到xxx.app目录下缘故,导致服务取不到path值,后来把web文件夹copyItemAtPath到documents根目录下就OK了。原因有个沙盒安全机制---------
因为应用的沙盒机制,应用只能在几个目录下读写文件
Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
tmp:提供一个即时创建临时文件的地方。
相关的获取目录方法:
1 //获取根目录 2 NSString *homePath = NSHomeDirectory(); 3 NSLog(@"Home目录:%@",homePath); 4 5 //获取Documents文件夹目录,第一个参数是说明获取Doucments文件夹目录,第二个参数说明是在当前应用沙盒中获取,所有应用沙盒目录组成一个数组结构的数据存放 6 NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 7 NSString *documentsPath = [docPath objectAtIndex:0]; 8 NSLog(@"Documents目录:%@",documentsPath); 9 10 //获取Cache目录 11 NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 12 NSString *cachePath = [cacPath objectAtIndex:0]; 13 NSLog(@"Cache目录:%@",cachePath); 14 15 //Library目录 16 NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 17 NSString *libPath = [libsPath objectAtIndex:0]; 18 NSLog(@"Library目录:%@",libPath); 19 20 //temp目录 21 NSString *tempPath = NSTemporaryDirectory(); 22 NSLog(@"temp目录:%@",tempPath);