I want to copy file from app NSBundle to Documents directory.
How ever i can’t get this code working:
-(NSString*)copyFile:(NSString*)name{
NSString *storePath = [[[appDelegate applicationDocumentsDirectory] absoluteString] stringByAppendingPathComponent:name];
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"recordTest" ofType:@"caf"];
if (defaultStorePath) {
if ([fileManager fileExistsAtPath:defaultStorePath]) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:&error];
NSLog(@"Error: %@)" , [error localizedDescription]);
}
}
}
return storePath;
}
This print The operation couldn’t be completed.
In more detail:
Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0xd629530 {NSUserStringVariant=(
Copy
), NSFilePath=/var/mobile/Applications/D0183043-3FD2-4341-8DA4-E1D140E556F6/test.app/recordTest.caf, NSDestinationFilePath=file:/localhost/var/mobile/Applications/D0183043-3FD2-4641-8DA4-E5D140E556F6/Documents/recordTest.caf, NSUnderlyingError=0xd629970 "The operation couldn’t be completed. No such file or directory"}
I don’t get why there are No such file if i check before copying.
Any ideas ?
Thanks
Your destination is a file URL, not a file path. I always use:
Or, in Swift 2: