Search code examples
iosobjective-ccocoa-touchplist

How can I delete a plist programmatically?


I want to delete my entire plist file from the filesystem programmatically. How can I do this?

This is how I am writing the plist:

+ (void)writeObjectToPList:(id)myData {  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];
    [myData writeToFile:path atomically:YES];
}  

Solution

  • NSFileManager will allow you to delete your file using removeItemAtPath:error:

    + (void)deletePList {  
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];
    
        NSError *error;
        if(![[NSFileManager defaultManager] removeItemAtPath:path error:&error])
        {
            //TODO: Handle/Log error
        }
    }