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
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.
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.
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.
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.
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]);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With