Delete specified file from document directory

前端 未结 10 939
礼貌的吻别
礼貌的吻别 2020-12-02 06:18

I want to delete an image from my app document directory. Code I have written to delete image is:

 -(void)removeImage:(NSString *)fileName
{
    fileManag         


        
10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 06:45

    I checked your code. It's working for me. Check any error you are getting using the modified code below

    - (void)removeImage:(NSString *)filename
    {
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
      NSString *filePath = [documentsPath stringByAppendingPathComponent:filename];
      NSError *error;
      BOOL success = [fileManager removeItemAtPath:filePath error:&error];
      if (success) {
          UIAlertView *removedSuccessFullyAlert = [[UIAlertView alloc] initWithTitle:@"Congratulations:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
          [removedSuccessFullyAlert show];
      }
      else
      {
          NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
      }
    }
    

提交回复
热议问题