Using the below line,
[fileManager copyItemAtPath:sourcePath toPath:targetPath error:&error];
NSData *myData = [NSData dataWithContentsOfURL:FileURL]; /fetch single file
[myData writeToFile:targetPath atomically:YES];
The default behavior of
NSFileManager method is to throw an exception/error "File Exists." when the file exists. But still if you want to overwrite using
NSFileManager then it provides one api for that which is mentioned below replaceItemAtURL as well in first solution:-
Also there are three solutions to achieve that
[filemanger replaceItemAtURL:url1 withItemAtURL:url2 backupItemName:@"/Users/XYZ/Desktop/test.xml" options:NSFileManagerItemReplacementUsingNewMetadataOnly resultingItemURL:nil error:nil];
Using the above API you can overwrite the file contents. But before that you have to take the backup of your source path in your temporary directory.
Already you have mentioned in your question using
trojanfoe has mentioned in their answer. i.e. remove the item being overwritten beforehand.