Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}  
like image 561
Sheehan Alam Avatar asked Aug 15 '11 17:08

Sheehan Alam


2 Answers

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
    }
}
like image 149
Joe Avatar answered Sep 22 '22 10:09

Joe


Here's how.

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];
    [fileManager removeItemAtPath: fullPath error:NULL];
like image 38
Manish Burman Avatar answered Sep 21 '22 10:09

Manish Burman