I am trying to copy an mp3 file from my Resources folder to a folder inside \"Documents\" folder of the app. On the simulator this works fine. But when I run it on a device,
EDIT:
Just figured out an even simpler solution. Instead of moveItemAtPath:toPath:error:, just use copyItemAtPath:toPath:error: ... since we truly want to copy the file from the mainBundle and not move it. I should have thought of that sooner!
E.g.
[[NSFileManager defaultManager] copyItemAtPath:mainBundleFilePath
toPath:destPath
error:&err]
See my previous comments below about why this works.
I believe I have the answer to this question. I can say for sure that the issue is not the destination file path.
I was getting the same Cocoa error 513 (NSFileWriteNoPermissionError) with pretty much the exact same code:
[[NSFileManager defaultManager] moveItemAtPath:mainBundleFilePath
toPath:destPath
error:&err]
The problem appears to be that the file, coming from the mainBundle, doesn't have suitable permissions to be moved to another place. I'm not sure if this command, if executed, would actually move the file from the mainBundle or just copy it...but either way, the file manager doesn't seem to like the idea.
The solution is simple: just read the mainBundle file into an NSData object and then write the NSData to a new file. Note the destination file path is the same in both examples, which shows us that lostInTransit is correct in saying that his file path is OK.
For this example, then, the following code will work and not throw an error:
NSData *mainBundleFile = [NSData dataWithContentsOfFile:mainBundleFilePath];
[[NSFileManager defaultManager] createFileAtPath:destPath
contents:mainBundleFile
attributes:nil];
BTW, in my own code, instead of passing a nil for attributes:, I set up an NSDictionary with a NSFileModificationDate attribute. I also wrapped the createFileAtPath:contents:attributes in an error handing if-statement. In other words,
if (![[NSFileManager defaultManager] createFileAtPath:destPath
contents:mainBundleFile
attributes:myAttributes]) {
// handle error as necessary, etc...
}
It took me a while to figure all of this out, so hopefully this solution will be helpful to others.
Are you sure you are getting the path to Documents folder correctly? The absolute path in the simulator is different than the absolute path on the device.
You should use the following to make sure you get the correct path to the Documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
The path for documentsDirectory on the the device would be something like:
/var/mobile/Applications/30B51836-D2DD-43AA-BCB4-9D4DADFED6A2/Documents
The path on the simulator would be something like:
/Volumes/Stuff/Users/johnDoe/Library/Application Support/iPhone Simulator/User/Applications/118086A0-FAAF-4CD4-9A0F-CD5E8D287270/Documents
You can read more on the File & Networking page on the dev site.
That's the NSFileWriteNoPermissionError
:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html
Somehow, you do have the wrong path and it's not letting you write there. You could also delete the app and try again, in case somehow your app documents directory was set to the wrong permissions...
I'd give us the line of code doing the copy, and print outs of each variable used in that line. Then we can see what the problem is.