Sheehan Alam Sheehan Alam - 4 years ago 85
Objective-C Question

How can I delete a plist programatically?

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];
}

Joe Joe
Answer Source

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
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download