Question

i am trying to delete a file at a particular URL, my code iS :

    NSString *str= [outputFieldURL absoluteString];

       NSError *error;

        BOOL success = [[NSFileManager defaultManager] removeItemAtPath:str error:&error];



        if (!success) {
            NSLog(@"Error removing file at path: %@", error.localizedDescription);
        }
        else
        {
            NSLog(@"File removed  at path: %@", error.localizedDescription);
        }
    }

and am getting output : Error removing file at path: The operation couldn’t be completed. (Cocoa error 4.)

where outputFieldURL is showing following value when i convert it into a string :
file:///var/mobile/Applications/A55A56FA-478D-4996-807D-12F0E968F969/Documents/301013125211w.m4a

this is the path where my audio with a format of .m4a is saved

Was it helpful?

Solution

Your path is incorrect

Use the following

NSString *str= [outputFieldURL path];

in place of

NSString *str= [outputFieldURL absoluteString];

The method "removeItemAtPath:" need the local path of file, If you want to remove using url, you should use "removeItemAtURL:"

OTHER TIPS

There may be case that file path which you provide is not correct. If its correct then try following with URL, it might solve your issue

    NSString *str= [outputFieldURL absoluteString];

    NSError *error;

    NSURL *url = [NSURL URLWithString:str];
    BOOL success = [[NSFileManager defaultManager] removeItemAtURL:url error:&error];


    if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
    else
    {
        NSLog(@"File removed  at path: %@", error.localizedDescription);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top