Vibol
Vibol

Reputation: 1168

iOS : cannot delete file

I'm newbie in iOS. I have a problem.

I log the path of file and I also verify it in Finder. But fileExistsAtPath: return NO, that's why I cannot delete it.

I need help please!!

Here is the code:

+ (void)removeImage:(NSString*)imgName {
MyLog(@"%@", [Tool getFileFullPath:imgName]);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:(NSString *)[Tool getFileFullPath:imgName]];
NSLog(@"Path to file: %@", [Tool getFileFullPath:imgName]);        
NSLog(@"File exists: %d", fileExists);
NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:[Tool getFileFullPath:imgName]]);
if (fileExists) 
{
    BOOL success = [fileManager removeItemAtPath:[Tool getFileFullPath:imgName] error:&error];
    if (!success) NSLog(@"Error: %@", [error localizedDescription]);
}

}

Path to file: /Users/vibolteav/Library/Application Support/iPhone Simulator/5.1/Applications/FD57CA70-14E4-442D-9CA5-DE7A7AD56A93/Documents/img/2053871632

File exists: 0

Is deletable file at path: 1

Upvotes: 1

Views: 1507

Answers (2)

Hardeep Singh
Hardeep Singh

Reputation: 942

Document Path..

   NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

   documentPath_ = [searchPaths objectAtIndex: 0];

Append File name...

  NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fileName];

   NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:fullPath]) 
       {
           NSError *error;

           if (![fileManager removeItemAtPath:fullPath error:&error]) {

           }
       }

Upvotes: 1

Nims
Nims

Reputation: 431

for remove file from document directory you use below code:

 -(void)removeOneImage:(NSString*)fileName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSLog(@"%@",fullPath);
    [fileManager removeItemAtPath: fullPath error:NULL];

}

Upvotes: 1

Related Questions