"The operation couldn’t be completed. (Cocoa error 512.)" - iphone

I have this code, which should be working perfectly, but I can't udnerstand why it isn't:
+(NSString *)writeImageToFile:(UIImage *)image {
NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f);
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Images/"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDirectory = NO;
BOOL directoryExists = [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
if (directoryExists) {
NSLog(#"isDirectory: %d", isDirectory);
} else {
NSError *error = nil;
BOOL success = [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error];
if (!success) {
NSLog(#"Failed to create directory with error: %#", [error description]);
}
}
NSString *name = [NSString stringWithFormat:#"%#.jpg", [JEntry generateUuidString]];
NSString *filePath = [path stringByAppendingPathComponent:name];
NSError *error = nil;
BOOL success = [fullImageData writeToFile:filePath options:NSDataWritingAtomic error:&error];
if (!success) {
NSLog(#"Failed to write to file with error: %#", [error description]);
}
return filePath;
}
It passed the directoryExists without an error, but when it gets to writeToFile, it gives me this error:
Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x5634ee0 {NSFilePath=/var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/18DAE0BD-6CB4-4244-8ED1-9031393F6DAC.jpg, NSUnderlyingError=0x5625010 "The operation couldn’t be completed. Not a directory"}
Any ideas why this might be?

I was able to reproduce your error when writing a file first in the path #"Documents/Images/", then trying to write the image using your code.
I think there are two possible scenarios for this:
1) You created that file by mistake at a previous execution of your app. This will be solved if you reset the simulator using the menu: iOS Simulator > Reset Contents and Settings, and uninstalling the app from your device: Long press > click on the x symbol.
2) There is some code somewhere else in your app that creates this file. If this is the case, you should find this code and remove it.

From FoundationErrors.h:
NSFileWriteUnknownError = 512
Try using withIntermediateDirectories:YES.

In my case a period '.' in the directory name (e.g. ~/Documents/someDir.dir/somefile) was the cause of the problem. I removed the offending character and the error disappeared.

Related

NSFileManager: Copy file not success

I want to copy files located at /Library to the folder /User/Library/AddressBook/Sample/,
I used:
[[NSFileManager defaultManager] copyItemAtPath: #"/Library/MyFile.mp3"
toPath: #"/User/Library/AddressBook/Sample/MyFile.mp3"
error: &error];
But I encountered an error that says `Operation could not be completed. No such file or
directory`
I am working on a jailbroken iPhone.
The directory:
/User/Library/AddressBook/Sample/
does not exist on a phone normally. Have you added the Sample subdirectory, before trying to copy the mp3 file into it?
With the NSFileManager methods, I would also recommend using the error object to help you debug:
NSError* error;
[[NSFileManager defaultManager] copyItemAtPath:#"/Library//MyFile.mp3" toPath: #"/User/Library/AddressBook/Sample/MyFile.mp3" error:&error];
if (error != nil) {
NSLog(#"Error message is %#", [error localizedDescription]);
}
Also, it looks like there is a mistake in your spelling of copyItemAtPath, but probably it's only in your question, and not in your code? Anyway, please double-check.
And, you have a double-slash (//) in your path, too, but I don't think that's hurting you. Just take it out, and be careful when typing :)
Update
If you are just running this app normally, but on a jailbroken phone, your app won't have access to those directories. Apps installed normally, on a jailbroken phone, still are sandboxed. The jailbreak doesn't remove all the rules on the phone. If you install the app in /Applications, like true jailbreak apps are, then that code should work for you.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSLog(#"%#",libraryDirectory); // Library path
NSString *AddressBookPath = [libraryDirectory stringByAppendingPathComponent:#"AddressBook"];
if (![[NSFileManager defaultManager] fileExistsAtPath:AddressBookPath])
{
NSError* error;
// Create "AddressBook Dir"
if([[NSFileManager defaultManager] createDirectoryAtPath:AddressBookPath withIntermediateDirectories:NO attributes:nil error:&error])
{
// Create "Sample Dir"
NSString *samplePath = [AddressBookPath stringByAppendingPathComponent:#"Sample"];
if (![[NSFileManager defaultManager] fileExistsAtPath:AddressBookPath])
{
NSError* error;
if([[NSFileManager defaultManager] createDirectoryAtPath:AddressBookPath withIntermediateDirectories:NO attributes:nil error:&error])
{
// Copy Files Now
NSError* error;
NSString *fromPath = [libraryDirectory stringByAppendingPathComponent:#"MyFile.mp3"];
NSString *toPath = [samplePath stringByAppendingPathComponent:#"MyFile.mp3"];
[[NSFileManager defaultManager] copyItemAtPath:fromPath toPath:toPath error:&error];
if (error != nil)
{
NSLog(#"Error message is %#", [error localizedDescription]);
}
}
}
}
else
{
NSLog(#"[%#] ERROR: attempting to write create MyFolder directory", [self class]);
NSAssert( FALSE, #"Failed to create directory maybe out of disk space?");
}
}

replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: broken in iOS 6?

I cannot get the NSFileManager method replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: to work in iOS 6. Apps that call this method and worked fine on iOS 5 have major issues on iOS 6. The problem does not occur on devices running versions of iOS below 6.0. The problem does not occur if the app is launched in the iOS Simulator by Xcode. Otherwise the problem seems to be universal.
Here is the test code I am trying to execute:
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:#"test.txt"];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"test.txt"];
// Create initial file in documents directory
if (![fileManager fileExistsAtPath:destinationPath])
{
BOOL fileCopied = [fileManager copyItemAtPath:sourcePath
toPath:destinationPath
error:&error];
if (!fileCopied)
[[self statusLabel] setText:[NSString stringWithFormat:#"Creation Error:\n\n%#",
[error localizedDescription]]];
}
// Replace file in documents directory with copy of file from app bundle
if ([fileManager fileExistsAtPath:destinationPath])
{
NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath];
BOOL fileReplaced = [fileManager replaceItemAtURL:destinationURL
withItemAtURL:[NSURL fileURLWithPath:sourcePath]
backupItemName:nil
options:0
resultingItemURL:&destinationURL
error:&error];
if (!fileReplaced)
[[self statusLabel] setText:[NSString stringWithFormat:#"Replacement Error:\n\n%#",
[error localizedDescription]]];
else
[[self statusLabel] setText:#"Successfully replaced file."];
}
It creates the file in the documents directory, if it doesn’t already exist. It then attempts to replace the file in the documents directory with a copy of a file from the app bundle. It then reports the status of the file creation/replacement. As I said before, it replaces fine if it’s being run on iOS 5 or lower or if it’s being run in the iOS Simulator with Xcode attached to the process. However, if it’s run on an iOS 6 device or the iOS Simulator without Xcode the replacement fails and an error is returned. The localized error description is The operation couldn’t be completed. (Cocoa error 512.).
The user info dictionary for the error is:
{
NSFileNewItemLocationKey = "file://localhost/var/mobile/Applications/487FBB9E-A2BD-4CF2-BB38-F36764623C2F/Test.app/test.txt";
NSFileOriginalItemLocationKey = "file://localhost/var/mobile/Applications/487FBB9E-A2BD-4CF2-BB38-F36764623C2F/Documents/test.txt";
NSURL = "file://localhost/var/mobile/Applications/487FBB9E-A2BD-4CF2-BB38-F36764623C2F/Documents/test.txt";
NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=513 \"The operation couldn\U2019t be completed. (Cocoa error 513.)\" UserInfo=0x1d58d350 {NSFilePath=/var/mobile/Applications/487FBB9E-A2BD-4CF2-BB38-F36764623C2F/Test.app/test.txt, NSURLUnsetValueKeysKey=<CFArray 0x1d58d180 [0x39b9d100]>{type = immutable, count = 2, values = (\n\t0 : <CFString 0x39b945b4 [0x39b9d100]>{contents = \"NSURLFileSecurityKey\"}\n\t1 : <CFString 0x39b943d4 [0x39b9d100]>{contents = \"NSURLCreationDateKey\"}\n)}, NSUnderlyingError=0x1d58d010 \"The operation couldn\U2019t be completed. Operation not permitted\", NSURL=file://localhost/var/mobile/Applications/487FBB9E-A2BD-4CF2-BB38-F36764623C2F/Test.app/test.txt}";
}
I have an app on the App Store which depends on this method. The live app continues to work without flaw on iOS 5, but on iOS 6 it is has huge problems due to the method failure. Does anyone know why this method is failing?
The NSFileManager method replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: is not a copy method; it is a move method. I.e., the file isn’t replaced with a copy of the replacement file, but with the replacement file itself. Since an app is not supposed to be able to modify its own bundle, the above code should never have worked in any version of iOS.
To retain atomicity, the solution is to first save a copy of the replacement file to the temporary directory, then replace the file with the copy in the temporary directory.
Here is the fixed test code:
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"txt"];
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:#"test.txt"];
// Create initial file in documents directory
if (![fileManager fileExistsAtPath:destinationPath])
{
BOOL fileCopied = [fileManager copyItemAtPath:sourcePath
toPath:destinationPath
error:&error];
if (!fileCopied)
[[self statusLabel] setText:[NSString stringWithFormat:#"Creation Error:\n\n%#", [error localizedDescription]]];
}
// Replace file in documents directory with copy of file from app bundle
if ([fileManager fileExistsAtPath:destinationPath])
{
// Create temporary file
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"test.txt"];
BOOL tempCopied = [fileManager copyItemAtPath:sourcePath
toPath:tempPath
error:&error];
if (!tempCopied)
[[self statusLabel] setText:[NSString stringWithFormat:#"Temp Creation Error:\n\n%#", [error localizedDescription]]];
// Replace file with temporary file
NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath];
BOOL fileReplaced = [fileManager replaceItemAtURL:destinationURL
withItemAtURL:[NSURL fileURLWithPath:tempPath]
backupItemName:nil
options:0
resultingItemURL:&destinationURL
error:&error];
if (!fileReplaced)
[[self statusLabel] setText:[NSString stringWithFormat:#"Replacement Error:\n\n%#", [error localizedDescription]]];
else
[[self statusLabel] setText:#"Successfully replaced file."];
}

Deleting/Overwriting Image files on iphone obj c

I am attempting to delete or overwrite an image within a specific directory. I grab all my images in an array and place in a table view. When an image is clicked photo roll is opened and the user chooses an image. when the imagepicker controller is being released I call out my methods to try and delete the original image and save the new image to the specific filepath. I receive a odd error when deleting the image and not sure how I can give a better log to save the image but it is not saving either. I would appreciate an input!
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"image picker did finish");
widgetImg.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
change.enabled = YES;
[self saveImage:widgetImg.image :ImgName];
NSString *path = [NSString
stringWithFormat:#"/Library/Themes/%#/iSetUp/UserPhotos/%#",selectedThemeName,ImgName];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:path];
NSLog(#"Path to file: %#", path);
NSLog(#"File exists: %d", fileExists);
NSLog(#"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:path]);
if (fileExists)
{
BOOL success = [fileManager removeItemAtPath:path error:&error];
if (!success) NSLog(#"Error: %#", [error localizedDescription]);
}
[self dismissModalViewControllerAnimated:YES];
}
- (void)saveImage:(UIImage*)image:(NSString*)imageName {
//convert image into .png format.
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *IMGPath = [NSString
stringWithFormat:#"/Library/Themes/%#/iSetUp/UserPhotos/",selectedThemeName];
[fileManager createFileAtPath:IMGPath contents:imageData attributes:nil];
NSLog(#"image saved");
}
2012-03-29 16:05:05.907 iSetUp[1258:707] image picker did finish
2012-03-29 16:05:09.046 iSetUp[1258:707] image saved
2012-03-29 16:05:09.047 iSetUp[1258:707] Path to file:
/Library/Themes/Modernistic/iSetUp/UserPhotos/IMG_0183.JPG
2012-03-29 16:05:09.048 iSetUp[1258:707] File exists: 1
2012-03-29 16:05:09.049 iSetUp[1258:707] Is deletable file at path: 0
2012-03-29 16:05:09.053 iSetUp[1258:707] Error: The operation couldn’t be completed.
(Cocoa error 513.)
You cannot directly access folders like that through the iOS SDK. You must save and read files using the system functions that return the proper path:
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
NSString *pathToMenuLoopFile = [rootPath stringByAppendingPathComponent:#"Menu Loop.mp3"];
If you are looking to load files that you include in your Xcode project, please take a look at this post: Loading data files in iPhone project
The file is not deletable, either due to an ownership or permissions issue. See the discussion of isDeletableFileAtPath in
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html

Copying file from bundle to documents not working (Cocoa error 512)

I am trying to simply copy my sqlite3 database to the documents directory. I get a Cocoa Error 512 and what I figured about that is, that it's not a valid directory (or something like that.
The database file is in my Resources folder in XCode. (The name of file is correct)
Here is the code I am trying to use:
-(void) checkAndCreateDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
//databasePath + databaseName is declared in the header
databaseName = #"WaypointDatabase.sql";
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
success = [fileManager fileExistsAtPath:databasePath];
if(success)
{
NSLog(#"Database exists");
return;
}
else
NSLog(#"Database does not exists");
NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:#"WaypointDatabase" ofType:#"sql"];
if(databasePathFromApp == nil)
{
NSLog(#"ERROR: IT IS NIL");
}
NSError* error = nil;
NSLog(#"Path in bundle:\n%#\n\n", databasePathFromApp);
NSLog(#"Path to copy to:\n%#\n\n", databasePath);
[fileManager copyItemAtPath:databasePathFromApp
toPath:databasePath error:&error];
[fileManager release];
if (error)
{
NSLog(#"%#\n\n", error);
NSLog(#"%#", [error userInfo]);
}
}
And the output to console that I get is:
2011-10-30 10:36:13.242 xxxx[6726:707] Database does not exists
2011-10-30 10:36:13.249 xxxx[6726:707] Path in bundle:
/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql
2011-10-30 10:36:13.252 xxxx[6726:707] Path to copy to:
/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents/WaypointDatabase.sql
2011-10-30 10:36:13.268 xxxx[6726:707] Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xe8b7cd0 {NSUserStringVariant=(
Copy
), NSFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql, NSDestinationFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents/WaypointDatabase.sql, NSUnderlyingError=0xe8b7ee0 "The operation couldn’t be completed. Not a directory"}
2011-10-30 10:36:13.272 xxxx[6726:707] {
NSDestinationFilePath = "/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents/WaypointDatabase.sql";
NSFilePath = "/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql";
NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=20 \"The operation couldn\U2019t be completed. Not a directory\"";
NSUserStringVariant = (
Copy
);
}
I figure this must be to do with getting the directory paths or something, but have been stuck on this for a few days now.
Take note, this works perfectly & without any errors on the simulator.
Where could I have gone wrong?
[UPDATE]
Could this be because the Documents-folder directory does not exist? How would I go about to create/check it?
[UPDATE 2]
I have done some other tests using
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *err = nil;
NSArray *dirArr = [fileManager contentsOfDirectoryAtPath:documentsDir error:&err];
NSLog(#"~~Contents:\n%#",dirArr);
NSLog(#"~~Error: \n%#",err);
And surprisingly it gave Cocoa Error 256 and also said "Not a directory". It is almost as if the Documents directory does not exist. But it does, according toNSSearchPathForDirectoriesInDomains`
Here is the output I got
2011-10-30 10:59:16.934 xxxx[6774:707] ~~Contents:
(null)
2011-10-30 10:59:16.936 xxxx[6774:707] ~~Error:
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x10052580 {NSUserStringVariant=(
Folder
), NSFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents, NSUnderlyingError=0x10054a50 "The operation couldn’t be completed. Not a directory"}
The problem got solved by setting a non standard Bundle ID in die info.plist
I used the Bundle ID from iTunes Connect for this specific app. Now everything works perfectly.
Try changing
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
for
[[NSBundle mainBundle] pathForResource:#"WaypointDatabase" ofType:#"sql"];
and check that this does not return nil to ensure that the is not what's going wrong apart from that it looks fine to me on first skim.

How to directly store SQLite data in XCode?

Searched on here and got some vague answers, so I thought i'd rephrase the problem to get some clearer answers-
Right now I have an SQL Lite db that reads/parses information from a pre-formatted .txt file. When I open the app, there is a slight 'lag' as the iDevice parses the info, then gets fetched for the iDevice. I'm just wondering if there's any way to just 'save' all the information directly in the xCode so there's no lag/fetch time?
Thank you.
What you can do is pre-build your sqlite database and then include it as a resource in your application. As this database is inside your application bundle it will be read only on your device so you will need to make a copy of it in your application document area.
I have successfully used something like the following code in a production iPhone application
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = [searchPaths objectAtIndex: 0];
NSString* dbFilePath = [documentFolderPath stringByAppendingPathComponent: DATABASE_FILE_NAME];
if(![[NSFileManager defaultManager] fileExistsAtPath: dbFilePath]) {
// copy the template database into the right place
NSError* error = nil;
[[NSFileManager defaultManager] copyItemAtPath: [[NSBundle mainBundle] pathForResource: #"template" ofType: #"db"] toPath: dbFilePath error: &error];
if(error) {
#throw [NSException exceptionWithName: #"DB Error" reason: [error localizedDescription] userInfo: nil];
}
}
int dbrc; // database return code
dbrc = sqlite3_open ([dbFilePath UTF8String], &db);
if(IS_SQL_ERROR(dbrc)) {
#throw [NSException exceptionWithName: #"DB Error" reason: NSLocalizedString(#"Could not open database", #"Database open failed") userInfo: nil];
}