I'm trying to copy a file from my application bundle to my app's documents directory. I'm getting an error, "Cocoa Error 262". What am I doing wrong? Here's my code:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData.sqlite"];
NSURL *initialURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"sqlite"]];
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:[initialURL absoluteString]]) {
NSLog(@"Original does not exist. \nPath: %@", [initialURL absoluteString]);
}
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL absoluteString]]) {
NSLog(@"Destination file does not exist. \nPath: %@", [storeURL absoluteString]);
[[NSFileManager defaultManager] copyItemAtURL:initialURL toURL:storeURL error:&error];
NSLog(@"Error: %@", [error description]);
}
Malware or viruses can corrupt or disable the clipboard. A large clipboard history that hasn't been cleared recently can cause problems. A faulty keyboard or mouse may not allow you to copy or paste correctly. Corrupt system files or drivers can interfere with the clipboard.
Cause: The Copy area and the Paste area are not the same size and shape. Solution: Select the upper-left cell instead of the whole range before you paste. Click the cell where you want the upper-left cell of the copied data to appear. On the Home tab, click Paste.
The problem is you're initializing a URL with a plain old file path.
NSURL *initialURL =
[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"CoreData"
ofType:@"sqlite"]];
Use [NSURL fileURLWithPath:]
instead.
The error you are getting is
NSFileReadUnsupportedSchemeError
Read error because the specified URL scheme is unsupported
which I believe would mean one of your paths is not forming correctly. perhaps write these paths to the log and see if they are coming out as you expect them to.
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