Note that there are some explanatory texts on larger screens.

plurals
  1. POreplaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: broken in iOS 6?
    text
    copied!<p>I cannot get the NSFileManager method <code>replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:</code> 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.</p> <p>Here is the test code I am trying to execute:</p> <pre><code>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:&amp;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:&amp;destinationURL error:&amp;error]; if (!fileReplaced) [[self statusLabel] setText:[NSString stringWithFormat:@"Replacement Error:\n\n%@", [error localizedDescription]]]; else [[self statusLabel] setText:@"Successfully replaced file."]; } </code></pre> <p>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 <code>The operation couldn’t be completed. (Cocoa error 512.)</code>.</p> <p>The user info dictionary for the error is:</p> <pre><code>{ 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=&lt;CFArray 0x1d58d180 [0x39b9d100]&gt;{type = immutable, count = 2, values = (\n\t0 : &lt;CFString 0x39b945b4 [0x39b9d100]&gt;{contents = \"NSURLFileSecurityKey\"}\n\t1 : &lt;CFString 0x39b943d4 [0x39b9d100]&gt;{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}"; } </code></pre> <p>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?</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