首先上代碼我們往沙盒存一個文件
-(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 中就可以看到我們保存到沙盒的文件了