1、xcode要拷贝的文件夹在本地目录中的添加方式:
(1)错误方式:
因为文件夹的拷贝可能包含多级目录,而直接从外部拖进来的文件夹默认在运行打包下的.ipa中无法以文件夹的形式存在,默认都在.ipa的根目录下,不是以文件夹的形式存在。
(2)正确方式:
需要在本地目录的所在的文件夹中创建文件夹,将需要拷贝的文件放入该文件夹中,以Add Files to "XXX"的形式添加入本地目录中,这样文件夹可以在运行打包下来的.ipa中以文件夹的形式存在。
2、代码实现文件夹拷贝到沙盒中:
- (void)copyFile
{
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/localData"];
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 创建一个文件管理器
NSFileManager *manager = [NSFileManager defaultManager];
NSString *filePath = [[pathArray firstObject] stringByAppendingPathComponent:@"/localData"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
//文件夹已存在
[SVProgressHUD showImage:nil status:@"文件夹已经存在"];
return;
} else {
NSError *e = nil;
BOOL isCopy = [manager copyItemAtPath:resourcePath toPath:filePath error:&e];
if (e) {
NSLog(@"move failed:%@",[e localizedDescription]);
}
if (isCopy) {
NSLog(@"拷贝成功");
} else {
NSLog(@"拷贝失败");
}
}
}