views:

95

answers:

1

Hi all.

I'v been struggling with this one for some time so any hint or suggestion are welcome.

I'm trying to delete a file from a directory under "Documents". The problem is that the file is not delete on the device dow it is on the simulator. Just to add to the mystery, before the call to removeItemAtPath I check if the file exists with fileExistsAtPath, and even display the list of files under that folder.

Attached the code for deletion:

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

// Delete file
NSString *directory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Documents/MyDir"];
NSString *path = [directory stringByAppendingPathComponent:[mListOfItems objectAtIndex:indexPath.row]];
NSError *error = nil;

// Check if FileName doesn't exist.
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"File Deletion Failed" message:[NSString stringWithFormat:@"File %@ not found.", [mListOfItems objectAtIndex:indexPath.row]]  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [alertView show];
 [alertView release];   
}
else {

  FAILED HERE ---V  
        if (![fileManager removeItemAtPath:path error:&error]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error While Deleting File" message:[NSString stringWithFormat:@"%@", error]  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];   

 }
 else {
// delete row from local data
[self.tableView beginUpdates];
[mListOfItems removeObjectAtIndex:indexPath.row];

// Delete the row from view
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
 }// Delete - success
}// File Exist
}// Edit - Delete   
}

The code that display the list of files in that folder...

   - (void) handleFilesRowTapped
 {

 // Get list of files to be displayed
 NSFileManager *manager = [NSFileManager defaultManager];
   NSError *error = nil;
 NSString * directory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Documents/MyDir"];
 NSArray *fileList = [manager contentsOfDirectoryAtPath:directory error:&error];

 // Allocate file list view controller
 FileListViewController *fileListViewController = [[FileListViewController alloc] initWithNibName:@"FileListViewController" bundle:nil];

 //// Make sure that only *.TXT files are displayed

 // Set the list to be displayed
 fileListViewController.mListOfItems = [[NSMutableArray alloc] init];
 for (NSString *fileName in fileList) {
  if (([[fileName substringWithRange:NSMakeRange([fileName length] - 4, 4)]     compare:@".txt"] == NSOrderedSame) || 
 ([[fileName substringWithRange:NSMakeRange([fileName length] - 4, 4)] compare:@".TXT"] == NSOrderedSame)){
   [fileListViewController.mListOfItems addObject:fileName];
  }
 }

 // Configure viwe controller
 [fileListViewController setTitle:@"Files"];

 // Push for display
 [self.navigationController pushViewController:fileListViewController animated:YES];
 [fileListViewController release];

    }

Many thanks.

+1  A: 

This:

[[NSBundle mainBundle] resourcePath]

... returns a path like:

~/Library/Application Support/iPhone Simulator/4.0.1/Applications/6FACD1C6-3E81-4FE1-97E0-F80604E134E0/i4TestBed.app

...which is the path to the app bundle itself which is read only. Once an app is complied its bundle contents are unalterable in anyway.

To find a file in the documents folder use:

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

... which returns a path like:

~/Library/Application Support/iPhone Simulator/4.0.1/Applications/6FACD1C6-3E81-4FE1-97E0-F80604E134E0/Documents

Append the file name and you are good to go.

TechZen