Monday, April 22, 2013

NSFileManager isDeletableFileAtPath example ios


isDeletableFileAtPath:

Returns a Boolean value that indicates whether the invoking object appears able to delete a specified file.
- (BOOL)isDeletableFileAtPath:(NSString *)path
Parameters
path
A file path.
Return Value of [NSFileManager isDeletableFileAtPath]
YES if the current process has delete privileges for the file at path; otherwise NO if the process does not have delete privileges or the existence of the file could not be determined.
Discussion of [NSFileManager isDeletableFileAtPath]
For a directory or file to be deletable, the current process must either be able to write to the parent directory of path or it must have the same owner as the item atpath. If path is a directory, every item contained in path must be deletable by the current process.
If the file at path is inaccessible to your app, perhaps because it does not have search privileges for one or more parent directories, this method returns NO. If the item at path is a symbolic link, it is not traversed.
Example of [NSFileManager isDeletableFileAtPath]
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    BOOL fileExists = [fileManager fileExistsAtPath:path];
    NSLog(@"Path to file: %@", path);        
    NSLog(@"File exists: %d", fileExists);
    NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:path]);
    if (fileExists) 
    {
        BOOL success = [fileManager removeItemAtPath:path error:&error];
        if (!success) NSLog(@"Error: %@", [error localizedDescription]);
    }