Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>EDIT:</em></p> <p>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!</p> <p>E.g.</p> <pre><code>[[NSFileManager defaultManager] copyItemAtPath:mainBundleFilePath toPath:destPath error:&amp;err] </code></pre> <p>See my previous comments below about why this works.</p> <hr> <p>I believe I have the answer to this question. I can say for sure that the issue is not the destination file path.</p> <p>I was getting the same Cocoa error 513 (NSFileWriteNoPermissionError) with pretty much the exact same code:</p> <pre><code>[[NSFileManager defaultManager] moveItemAtPath:mainBundleFilePath toPath:destPath error:&amp;err] </code></pre> <p>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 <em>move</em> the file from the mainBundle or just <em>copy</em> it...but either way, the file manager doesn't seem to like the idea.</p> <p>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.</p> <p>For this example, then, the following code will work and not throw an error:</p> <pre><code>NSData *mainBundleFile = [NSData dataWithContentsOfFile:mainBundleFilePath]; [[NSFileManager defaultManager] createFileAtPath:destPath contents:mainBundleFile attributes:nil]; </code></pre> <p>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, </p> <pre><code>if (![[NSFileManager defaultManager] createFileAtPath:destPath contents:mainBundleFile attributes:myAttributes]) { // handle error as necessary, etc... </code></pre> <p>} </p> <p>It took me a while to figure all of this out, so hopefully this solution will be helpful to others.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload