2011-11-08 11 views
1

アプリケーションの起動時にNSBundleからDocumentsディレクトリに複数のファイルをコピーしたいと思います。 「applicationDidFinishLaunchingWithOptions」メソッドに複数のファイルを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メソッドが呼び出されます。

この

は、ファイルをコピーするためのコードです。しかし、この方法では、1つのファイルをDocumentsディレクトリにコピーすることしかできません。 NSBundleから50個のファイルをコピーしたい場合は、50個のパスを指定する必要がありますか?コードのほんの数行でNSBundleのすべてのファイルを取得するなどの方法がありますか?

答えて

0
あなたの機能プロトタイプ変更

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

なぜか分かりますか? – Lloydworth

+0

@Lloydworthそれで、あなたは 'copyFileWithName'を50回呼び出すことができるので、毎回別のファイル名を与え、' copyFileWithName'は毎回 'getPathOfFileName'を別のファイル名で呼び出して、コピーされた現在のファイルのパスを取得できます。もちろん、これはまだあなたが50個のファイルすべてのリストを必要としていることを意味します。だから、これは本当に便利な解決策ではありません。 – Mecki

0

ええを確認してくださいあなたはそれを

- (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]); 
      } 
     } 
    } 
} 
6

を行うことができますあなたのリソースフォルダからすべてのファイルをコピーすることができますが、私はあなたが本当にそれをやりたい疑い、あなたは?これらはすべてアプリケーションリソース(UIボタンなどの文字列ファイルやグラフィックスなど)です

リソースフォルダにサブディレクトリ(例:FilesToCopy)がありますコピーするだけで、あなたは一度に一つのファイルをコピーすることができます

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]; 

を使用し、ディレクトリの内容を取得するには

(例えばPhoto.pngContents/Resources/FilesToCopy/Photo.pngで発見された):

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]; 
} 
+0

これは実際に質問に対する正解です。ありがとうMecki。 – Bms270

+0

覚えておくべき重要なことは、プロジェクトにドラッグするときに、ダイアログボックスから[フォルダ参照の作成]を選択することです。グループを作成しただけでは、グループであるためコードでフォルダが見つからないため、ファイルが列挙されず、コピーも行われません。 –

関連する問題