Search code examples
iosnsdatawritetofile

Deleting file from device after using [NSData writeToFile:]


In my code I'm doing:

[anNsDataObject writeToFile:@"thefile" atomically:YES];

How can I delete this file from my device?


Solution

  • Using NSFileManager's removeItemAtPath:error: method.

    Example:

    NSError *error;
    [[NSFileManager defaultManager]removeItemAtPath:@"theFile" error:&error];
    
    if (error)
    {
      // file deletion failed
    }