iPhone: Error in copyItemAtPath

11,759

Solution 1

Issue 1: The issue occurred because, the directory already contains a file with same file name.

You should check whether the file exist in the folder or not like:

- (void)saveFile:(NSString *)folderPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"destination.png"];

    NSError *error = nil;
    NSString *srcPath = [folderPath stringByAppendingPathComponent:@"filename.png"];

    if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    {
        //removing file
        if (![[NSFileManager defaultManager] removeItemAtPath:dataPath error:&error])
        {
            NSLog(@"Could not remove old files. Error:%@",error);
        }
    }
    BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dataPath error:&error];
    if (success == YES)
    {
        NSLog(@"Copied");
    }
    else
    {
        NSLog(@"Not Copied %@", error);
    }
}

Issue 2: You must provide a file name not a directory.

Replace:

BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:documentsDirectory error:&error];

With:

BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dataPath error:&error];

Solution 2

copyItemAtPath: is giving error as that file is already exist in that location. So you should do this before you copy file.

if ([[NSFileManager defaultManager] fileExistsAtPath: srcPath])
        [[NSFileManager defaultManager] removeItemAtPath: srcPath error:nil];
Share:
11,759
EXC_BAD_ACCESS
Author by

EXC_BAD_ACCESS

Updated on September 06, 2022

Comments

  • EXC_BAD_ACCESS
    EXC_BAD_ACCESS over 1 year

    I am trying to copy a file from one folder to another folder in documents directory like this:

        - (void)saveFile:(NSString *)folderPath
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; 
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"destination.png"];
    
        NSError *error = nil;
        NSString *srcPath = [folderPath stringByAppendingPathComponent:@"filename.png"];
    
    
        BOOL success = [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:documentsDirectory error:&error];
        if (error)
        {
            NSLog(@"%@",error);
        }
    
        if (success == YES)
        {
            NSLog(@"Copied");
        }
        else
        {
            NSLog(@"Not Copied");
        }
    }
    

    When i log error, it gives me the following message:

    Error Domain=NSCocoaErrorDomain Code=516 "The operation couldn’t be completed. (Cocoa error 516.)" UserInfo=0x9d665a0 {NSUserStringVariant=(
        Move
    ), NSFilePath=/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/BC37C9D7-7995-47C1-8131-2B07BADCBECB/Documents/foldername/B713320C-2CA0-4FD3-93F6-71D76B102B83/src.png, NSDestinationFilePath=/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/BC37C9D7-7995-47C1-8131-2B07BADCBECB/Documents, NSUnderlyingError=0x9d41c60 "The operation couldn’t be completed. File exists"}