//把 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 目录下
最新推荐文章于 2018-08-03 18:21:48 发布