iOS把Resource文件夹下的文件拷贝到沙盒

2019-02-12  本文已影响12人  晴天ccc

前言:

我们Xcode项目工程里面有一个文件,以model-o.scnassets.zip为例,想复制到沙盒文件中。具体操作如下:

 // Resource文件夹下的文件

NSString * docPath = [[NSBundle mainBundle] pathForResource:@"model-o.scnassets" ofType:@".zip"];

 // 沙盒路径

NSString *appLib = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] ;

// 执行操作 

BOOLfilesPresent = [selfcopyMissingFile:docPathtoPath:appLib];

 if(filesPresent) {  NSLog(@"迁移OK");  }else{  NSLog(@"迁移NO");  [selfsetDirBaseSetting]; }

// 把Resource文件夹下的文件拷贝到沙盒

- (BOOL)copyMissingFile:(NSString*)sourcePath toPath:(NSString*)toPath{

    BOOL retVal = YES; // If the file already exists, we'll return success…

    NSString * finalLocation = [toPath stringByAppendingPathComponent:[sourcePath lastPathComponent]];

    if (![[NSFileManager defaultManager] fileExistsAtPath:finalLocation])

    {

        retVal = [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:finalLocation error:NULL];

    }

    returnretVal;

}

完整代码(上面代码已经贴出):

上一篇下一篇

猜你喜欢

热点阅读