NSFileManager API has different behavior on different iOS versions

I noticed that copyItemAtURL:toURL:error: and moveItemAtURL:toURL:error: methods have different behavior on iOS 14 beta 7.

For example:
Method - (BOOL)copyItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL error:(NSError * _Nullable *)error;

From Apple documentation: "If a file with the same name already exists at dstURL, this method stops the copy attempt and returns an appropriate error."

On devices with iOS 13 this method works as expected. It returns an error because the file already exists at dstURL.
But on iOS 14 it does the opposite. It copies the file without errors.

Code sample:
Code Block
NSData *fileContents = [@"Put this in a file" dataUsingEncoding:NSUTF8StringEncoding];
NSString *directory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [directory stringByAppendingPathComponent:@"test.txt"];
NSFileManager *manager = [NSFileManager defaultManager];
[manager setDelegate:self];
[manager createFileAtPath:filePath
contents:fileContents
attributes:nil];
NSError *error = nil;
NSURL *url = [NSURL fileURLWithPath:filePath];
BOOL result = [manager copyItemAtURL:url toURL:url error:&error];



I noticed that -copyItemAtURL:toURL:error: and
-moveItemAtURL:toURL:error: methods have different behavior on iOS
14 beta 7.

In your snippet you’re copying a file on to itself. Was that deliberate? Is that the old case where you’re seeing the behaviour change?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer
Yes, it's deliberate. The copyItemAtURL:toURL:error: method was updated in beta iOS 14 so I just want to clarify if it's a bug or permanent changes in iOS 14.
You are deliberately copying a file to itself? Why?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
NSFileManager API has different behavior on different iOS versions
 
 
Q