每一个应用程序都会有一个这样的沙盒文件:
下面详细介绍如何获取沙盒中的文件路径:
//NSHomeDirectory() 表示取得当前xxx.app文件所在的根目录
NSString *homeDir = NSHomeDirectory();NSLog(@" homeDir is %@",homeDir);
///Users/yao/Library/Developer/CoreSimulator/Devices/5EC6F579-B784-42A3-A875-6FE1AEF68583/data/Containers/Data/Application/C5621CB2-013F-4FE7-96A2-73B5592BA9E4
//取得应用程序目录
NSString *appDir = [[NSBundle mainBundle]bundlePath];
NSLog(@"appDir is %@",appDir);
///Users/yao/Library/Developer/CoreSimulator/Devices/5EC6F579-B784-42A3-A875-6FE1AEF68583/data/Containers/Bundle/Application/B20C014B-1A45-41E3-B3BD-55D53B78C269/UINavignonBar.app
//获得Documents所在的目录数组
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *Documents = [paths objectAtIndex:0];
NSLog(@"Documents is %@",Documents);
///Users/yao/Library/Developer/CoreSimulator/Devices/5EC6F579-B784-42A3-A875-6FE1AEF68583/data/Containers/Data/Application/BFDE472C-117B-4104-86C0-FEDF4F2C6E90/Documents
//NSBundle 获取xxx.app文件资源内容的一种方式
//第一种方式:添加图片文件时选 Create groups 时:
//这种方式添加的资源文件夹颜色为黄色, 该文件夹是假的文件夹,在实际xxx.app中并不存在黄色的文件夹,而是直接把图片资源放到xxx.app文件夹下,所以此方法图片文件名必须不能重复。
//方法一获取
//Users/yao/Library/Developer/CoreSimulator/Devices/5EC6F579-B784-42A3-A875-6FE1AEF68583/data/Containers/Data/Application/C03145E0-2D37-4048-AB6A-0A963A46867D/UINavignonBar.app/pic1.png
//方法二获取
NSString *path11 = [[NSBundle mainBundle]pathForResource:@"pic1.png" ofType:nil];
///Users/yao/Library/Developer/CoreSimulator/Devices/5EC6F579-B784-42A3-A875-6FE1AEF68583/data/Containers/Data/Application/C03145E0-2D37-4048-AB6A-0A963A46867D/UINavignonBar.app/pic1.pngNSLog(@"path1 is %@ \n path11 is %@",path1,path11);
//第二种方式:添加资源文件时选 create folder references 时:
//方法一获取
NSString *path2 = [[NSBundle mainBundle]pathForResource:@"pic1" ofType:@"png" inDirectory:@"images"];
///Users/yao/Library/Developer/CoreSimulator/Devices/5EC6F579-B784-42A3-A875-6FE1AEF68583/data/Containers/Data/Application/0FF265CA-1553-4383-9B42-910EBF4EA692/UINavignonBar.app/images/pic1.png //方法二获取
NSString *path22 = [[NSBundle mainBundle]pathForResource:@"pic2.png" ofType:nil inDirectory:@"images"];
///Users/yao/Library/Developer/CoreSimulator/Devices/5EC6F579-B784-42A3-A875-6FE1AEF68583/data/Containers/Data/Application/0FF265CA-1553-4383-9B42-910EBF4EA692/UINavignonBar.app/images/pic2.png//方法三获取
NSString *path222 = [[NSBundle mainBundle]pathForResource:@"images/pic2.png" ofType:nil inDirectory:nil];
//Users/yao/Library/Developer/CoreSimulator/Devices/5EC6F579-B784-42A3-A875-6FE1AEF68583/data/Containers/Data/Application/B8A6AE88-6895-4B02-AB63-6D6E969F0770/UINavignonBar.app/images/pic2.pngNSLog(@"path2 is %@ \n path22 is %@ path222 %@",path2,path22,path222);