ios真机访问沙盒数据 获取沙盒数据

首先上代码我们往沙盒存一个文件

-(void)WriteToSandbox{
        UIView *v = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
        v.backgroundColor = [UIColor purpleColor];
        [self.view addSubview:v];
    
    //Document目录 documents (Documents)
    NSArray *paths =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path = [paths objectAtIndex:0];
    NSLog(@"%@",path);
    
//    //Libaray目录 various documentation, support, and configuration files, resources (Library)
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
//    NSString *path = [paths objectAtIndex:0];
//
//    //Cache目录 location of discardable cache files (Library/Caches)
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
//    NSString *path = [paths objectAtIndex:0];
    
    //通过归档把v 转成NSData
        NSData *viewData = [NSKeyedArchiver archivedDataWithRootObject:v];
    
        [viewData writeToFile:[NSString stringWithFormat:@"%@/view.arch",path] atomically:YES];
    


}

存到沙盒的documents文件夹下,下面我们真机跑程序,切记是真机而不是模拟器。

跑起来以后,应该文件已经存到沙盒了,然后我们看一下如何去拿到沙盒中的文件

按照上图操作,存到指定的文件夹

找到存储的文件,显示包内容

然后在appdata --  documents 中就可以看到我们保存到沙盒的文件了

猜你喜欢

转载自blog.csdn.net/lee727n/article/details/94741775