Search code examples
ios6nsfilemanagernsdocumentdirectory

PDF file not getting deleted in iOS


I am creating PDF page by following link...

http://mobile.tutsplus.com/tutorials/iphone/generating-pdf-documents/?search_index=3

I am not able to delete this PDF file using below method. I have commented error line...

self.fileMgr = [NSFileManager defaultManager];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];

 NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",[self.tablePdfListArray objectAtIndex:indexPath.row]]];

 if([self.fileMgr fileExistsAtPath:pdfPath] == YES)
 {

 [fileMgr removeFileAtPath: pdfPath error:nil];  //No visible @interface for NSFilemanager declares the selector removeFileAtPath 
 }

Could you please suggest. Thanks in advance.


Solution

  • I can't find removeFileAtPath:error: method anywhere in the class reference of NSFileManager. It seems to be a very old instance method. There is a similar method removeFileAtPath:handler: that seems to be deprecated.

    Try to use the removeItemAtPath:error: instead. From the class reference of NSFileManager:

    removeItemAtPath:error:

    Removes the file or directory at the specified path.

    You should be familiar with the usage. I would suggest you to assign the error param to NSError variable so that you can check the NSError at the end of the operation, just in case:

    NSError *error = nil;
    BOOL deleted = [[NSFileManager defaultManager] removeItemAtPath:pdfPath error:&error];
    if (!deleted) {
        NSLog(@"Unable to delete pdf at %@, reason: %@", path, error);
    }