沙盒机制

1.访问documents文件夹(太大的文件夹不要直接放在这个文件夹下面,上线是时候有可能审核被拒)

    //存储用户数据,需要备份的数据

    NSArray * documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    for (int i = 0; i < documentArray.count; i++) {
        NSLog(@"%@",documentArray[i]);
    }
    //参数的意思
    //第一个参数的意思是决定存放在哪个文件夹的下面
    //第二个参数类似于磁盘的概念(ABCD盘)
    //第三个参数是路径,yes是绝对路径,no是相对路径
    
    //通常情况下,路径返回的值是数组,由于数组里面只有一个元素,通常我们可以使用[arr lastObject]或者[arr firstObject]来打印路径
    NSString * documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@",documentPath);

2.访问library文件夹:下面有两个文件夹,一般我们不直接操作这个文件夹

    NSString * libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@",libraryPath);
    //cache文件夹:
    //文件缓存,程序专用的支持文件
    NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, NO) lastObject];
    NSLog(@"%@",cachePath);
    
    //Preferences文件夹(存储应用程序的偏好设置)
    NSString * preferPath = [NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, NO) lastObject];
    NSLog(@"%@",preferPath);
    
3.tmp文件夹,存储临时数据
    NSString * tmpPath = NSTemporaryDirectory();
    NSLog(@"%@",tmpPath);
    
    //主路径:document,library,tmp都存放在这个路径下面
    NSString * homePath = NSHomeDirectory();
    NSLog(@"%@",homePath);
4..app(bundle)
    //iOS8之前,bundle和document,library,tmp统一放在一个文件夹下面
    //iOS8之后,bundle路径发生了变化,直接放在container文件夹下面
    NSString * bundlePath = [[NSBundle mainBundle] resourcePath];
    NSLog(@"%@",bundlePath);
    
    NSString * imgPath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];
    NSLog(@"%@",imgPath);

posted @ 2015-10-30 19:46  宋婷婷  阅读(228)  评论(0编辑  收藏  举报