Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone (iOS): copying files from main bundle to documents folder error

I am trying to copy some files across from my app bundle to the documents directory on first launch. I have the checks in place for first launch, but they're not included in code snippet for clarity. The problem is that I am copying to the documents directory (which already exists) and in the documentation, it states that:

dstPath must not exist prior to the operation.

What is the best way for me to achieve the copying straight to the documents root? The reason I want to do this is to allow iTunes file sharing support.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Populator"];

  NSLog(@"\nSource Path: %@\nDocuments Path: %@", sourcePath, documentsDirectory);

  NSError *error = nil;

  if([[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:documentsDirectory error:&error]){
    NSLog(@"Default file successfully copied over.");
  } else {
    NSLog(@"Error description-%@ \n", [error localizedDescription]);
    NSLog(@"Error reason-%@", [error localizedFailureReason]);
  }
  ...
  return YES;
}

Thanks

like image 266
Jack Avatar asked Jul 14 '10 12:07

Jack


People also ask

Why does it say error copy file or folder?

Possible Causes of Unspecified Error in WindowsThe Target disk has not enough free space: if there is not enough free space and the files that you want to copy is very large, the data copy won't be completed. Target disk is corrupted, or the file is encrypted. System limitation. File or folder ownership changed.

How do I move documents into folders on my iPhone?

Tip: To move a document into a nearby folder (in the same location), touch and hold the document, and when it appears to lift, drag it to the folder.

Does iPhone have a Documents folder?

In the Files app, you can find: Files on the iPhone, iPad, or iPod touch that you're using. Files in iCloud Drive, including Pages, Numbers, and Keynote documents. Files in other cloud services and apps, like Box, Dropbox, OneDrive, Adobe Creative Cloud, Google Drive, and more.

How do I copy files in iOS app?

Tap Move to transfer items to another cloud storage device or your iPad. Tap the destination and then tap the folder (if available). Tap Copy to move the file. The files are copied to the destination.


2 Answers

Your destination path must contain the name of item being copied, not just the documents folder. Try:

if([[NSFileManager defaultManager] copyItemAtPath:sourcePath 
          toPath:[documentsDirectory stringByAppendingPathComponent:@"Populator"]
          error:&error]){
...

Edit: Sorry misunderstood your question. Don't know if there's a better option then iterating through folder contents and copy each item separately. If you're targeting iOS4 you can use NSArray's -enumerateObjectsUsingBlock: function for that:

NSArray* resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:copyItemAtPath:sourcePath error:NULL];
[resContents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
    {
        NSError* error;
        if (![[NSFileManager defaultManager] 
                  copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj] 
                  toPath:[documentsDirectory stringByAppendingPathComponent:obj]
                  error:&error])
            DLogFunction(@"%@", [error localizedDescription]);
    }];

P.S. If you can't use blocks you can use fast enumeration:

NSArray* resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:copyItemAtPath:sourcePath error:NULL];

for (NSString* obj in resContents){
    NSError* error;
    if (![[NSFileManager defaultManager] 
                 copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj] 
                 toPath:[documentsDirectory stringByAppendingPathComponent:obj]
                 error:&error])
            DLogFunction(@"%@", [error localizedDescription]);
    }
like image 112
Vladimir Avatar answered Sep 23 '22 11:09

Vladimir


a note:
don't issue lengthy operation in didFinishLaunchingWithOptions: is a conceptual mistake. If this copy takes too much time, watchdog will kill you. launch it in a secondary thread or NSOperation... I personally use a timer proc.

like image 33
ingconti Avatar answered Sep 26 '22 11:09

ingconti