//把 xxx.app 包里的文件拷贝到 Documents 目录下,并指定是否覆盖,YES 则覆盖
- (void)BundleToDocuments:(NSString *)fileName existsCover:(BOOL)cover
{
BOOL success;
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];//找到 Documents 目录
NSString *targetPath = [documentsDirectory stringByAppendingPathComponent:fileName];
if(!cover)
{
success = [fileManager fileExistsAtPath:targetPath];
if (success) return;
}else{
[fileManager removeItemAtPath:targetPath error:&error];
}
//把 xxx.app 包里的文件拷贝到 targetPath
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
//如果文件存在了则不能覆盖,所以前面才要先把它删除掉
success = [fileManager copyItemAtPath:bundlePath toPath:targetPath error:&error];
if(!success)
NSLog(@"'%@' 文件从 app 包里拷贝到 Documents 目录,失败:%@", fileName, error);
else
NSLog(@"'%@' 文件从 app 包里已经成功拷贝到了 Documents 目录。", fileName);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//#warning 发布的时候记得把 existsCover 改为 NO,不能每次启动都覆盖数据库文件
[self BundleToDocuments:@"HKWebsite.sqlite" existsCover:NO];
_projects = [[Projects alloc]init];
return YES;
}拷贝 xxx.app [NSBundle mainBundle] 包里的文件到 Documents 目录下
iOS应用文件批量导入Documents目录
最新推荐文章于 2017-09-28 21:06:18 发布
本文介绍如何使用Objective-C代码将iOS应用内的文件批量复制到Documents目录,并提供了覆盖现有文件的功能控制。
2213

被折叠的 条评论
为什么被折叠?



