簡體   English   中英

將多個文件復制到文檔目錄

[英]Copy multiple files to Documents directory

我想在應用程序啟動時將多個文件從我的NSBundle復制到Documents目錄。

這是復制文件的代碼:

- (NSString *) getPath 
{
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
  NSString *documentsDir = [paths objectAtIndex:0];
  return [documentsDir stringByAppendingString:@"Photo.png"];
}

- (void) copyFile 
{
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSError *error;
  NSString *Path = [self getPath];
  BOOL success = [fileManager fileExistsAtPath:Path];

  if(!success) {

    NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Photo.png"];
    success = [fileManager copyItemAtPath:defaultPath toPath:Path error:&error];

    if (!success)
        NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);
  }
}

copyFile方法將在“ applicationDidFinishLaunchingWithOptions”方法中調用。 但是此方法僅允許將一個文件復制到Documents目錄。 如果我想從NSBundle復制50個文件,這是否意味着我必須指定50個路徑? 有沒有更短的方法,例如僅用幾行代碼即可獲得NSBundle中的所有文件?

您可以從資源文件夾中復制所有文件,但是我懷疑您確實想這樣做,對嗎? 畢竟,這些也是應用程序資源(例如字符串文件或僅用於UI按鈕的圖形等)

所以,你可能想在你的資源文件夾中的子目錄(如命名FilesToCopy ),所有要復制的文件(例如Photo.png在發現Contents/Resources/FilesToCopy/Photo.png

要獲取目錄的內容,只需使用

NSError * err;
NSArray * files;
NSString * srcPath;
NSString * dstPath;
NSFileManager * man;

srcPath = [self getSrcPath];
dstPath = [self getDstPath];
man = [[NSFileManager alloc] init];
files = [man contentsOfDirectoryAtPath:srcPath error:&err];

然后您可以一次復制一個文件:

for (NSString *aFile in files) {
    BOOL success;

    NSString * srcFile = [srcPath stringByAppendingPathComponent:aFile];
    NSString * dstFile = [dstPath stringByAppendingPathComponent:aFile];
    success = [man copyItemAtPath:srcFile toPath:dstFile error:&err];
    // Verify success
}

現在,您只需要srcPathdstPath

- (NSString *)getSrcPath
{
     return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"FilesToCopy"];
}

- (NSString *)getDstPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

更改函數的原型:

- (NSString *) getPathofFileName:(NSString*)name;
- (void) copyFileWithName:(NSString*)name;

是的,肯定可以做到

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    NSMutableArray *filesArray = [NSMutableArray arrayWithObjects:@"0.epub",@"1.epub",@"2.epub",@"3.epub",@"4.epub",nil];//here put the name of files you want to copy
    [self copyFilesToDocuments:filesArray];

    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
    self.window.rootViewController = (UIViewController*)self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void) copyFilesToDocuments : (NSMutableArray*)filesArr {
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,        NSUserDomainMask, YES) lastObject];

    for(int i = 0 ; i < [filesArr count] ; i++) {
        NSString *filePath = [docDir stringByAppendingPathComponent:[filesArr objectAtIndex:i]];
        NSFileManager *fm = [NSFileManager defaultManager];
        BOOL exist = [fm fileExistsAtPath:filePath];
        NSError *error = nil;

        if (!exist) {
            NSString *path = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:[filesArr objectAtIndex:i]];
            BOOL success = [fm copyItemAtPath:path toPath:filePath error:&error];
            if (!success) {
                NSAssert1(0,@"%@",[error localizedDescription]);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM