iOS在沙盒Documents中创建文件夹以及获取沙盒路径

时间:2022-04-18 22:10:29

沙盒中创建文件夹

NSFileManager *fileManager = [[NSFileManager alloc]init];
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *createPath = [NSString stringWithFormat:@"%@/collect_deals.data",pathDocuments];
if (![[NSFileManager defaultManager]fileExistsAtPath:createPath]) {

[fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];

}else{
NSLog(@"有这个文件了");
}

这样我们就在沙盒Documents中创建好了名为collect_deals.data的文件夹。那我们现在来获取沙盒路径

 NSLog(@"%@",[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"collect_deals.data"]);

我们获得到类似如下路径
/User/SYH/Library/Developer/CoreSimulator/Devices/5B8713E8-CD24-4E78-90F3-8F8DA0D2B035/data/Containers/Data/Application/863172CE-8ACC-460B-B8F7-E897BF059DDE/Documents/collect_deals.data

我们打开Finder前往文件夹 ,现在是否在沙盒中就有了这个文件夹呢?是不是很简单!