博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转】从bundle中复制文件到Documents目录中的代码
阅读量:6974 次
发布时间:2019-06-27

本文共 2343 字,大约阅读时间需要 7 分钟。

下面的代码段转自这个网站,备用:

 

16. Copy a file from your app bundle to Documents

You may want to include files with your bundle that get copied into your documents folder in the sandbox. The following code is adapted from the Apple "SQLiteBooks" example:

- (void)makeDocumentSubdir:(NSString *)subdirname

{
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // set up the basic directory path name

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    // create the directory path name for the subdirectory

    NSString *subdirectory = [paths objectAtIndex:0];
    subdirectory = [documentsDirectory stringByAppendingPathComponent:subdirname];
    success = [fileManager createDirectoryAtPath:subdirectory withIntermediateDirectories:YES attributes:nil error:NULL ];
}

- (void)copyFileNamed:(NSString *)filename intoDocumentsSubfolder:(NSString *)dirname

{
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    // set up the basic directory path name
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    // set up the directory path name for the subdirectory

    NSString *subdirectory = [documentsDirectory stringByAppendingPathComponent:dirname];

    // set up the full path for the destination file

    NSString *writableFilePath = [subdirectory stringByAppendingPathComponent:filename];
    success = [fileManager fileExistsAtPath:writableFilePath];

    // if the file is already there, just return

    if (success)
            return;
    // The file not exist, so copy it to the documents flder.
    NSString *defaultFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename];
    success = [fileManager copyItemAtPath:defaultFilePath toPath:writableFilePath error:&error];
    if (!success) {
            //[self alert:@"Failed to copy resource file"];
            NSAssert1(0, @"Failed to copy file to documents with message .", [error localizedDescription]);
    }
}

- (void)firstRunSetup
{
    [self makeDocumentSubdir:@"FileDir1"];
    [self copyFileNamed:@"FirstFile.sqlite" intoDocumentsSubfolder:@"FileDir1"];
    [self copyFileNamed:@"SecondFile.sqlite" intoDocumentsSubfolder:@"FileDir1"];
}

转载地址:http://sresl.baihongyu.com/

你可能感兴趣的文章
pytorch-0.2成功调用GPU:ubuntu16.04,Nvidia驱动安装以及最新cuda9.0与cudnnV7.0配置
查看>>
6Python全站之路系列之Django站点管理
查看>>
bootstrap-进度条--动态条纹进度条
查看>>
事务xmin,xmax
查看>>
日本科学家的AI读心术,解码脑电波,还原人眼所见
查看>>
基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境
查看>>
案例推荐《南京银行: 云上打造FinTech生态圈》
查看>>
ASP.NET实现Cookie功能的三个基本操作(写入,读取,删除)
查看>>
直播和VR的代入感太强,淘宝也忍不住推出了VR直播
查看>>
安卓Textview的getLineCount返回0
查看>>
【无监督学习最新研究】简单的「图像旋转」预测,为图像特征学习提供强大监督信号...
查看>>
3星|《商业周刊/中文版:2017商业人物(上)》:全球的数据只有20%是可搜索的...
查看>>
关于iChartjs在移动端提示框tip显示不正常的解决方法
查看>>
我的Nginx配置文件
查看>>
Linux命令(33):netstat命令-显示网络端口信息
查看>>
Windows 2008 R2 Administrator access denied解决办法
查看>>
Faker:Python的伪造数据生成器
查看>>
JSON的三个好处
查看>>
MySQL的权限
查看>>
(桌面虚拟化最佳实践--呼叫中心系统优化之四)瘦终端优化项目与其他优化项目...
查看>>