Search code examples
ioscocoansfilemanager

unable to delete my file in ios


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


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:"