ios - MFMailComposeViewController losing attachments after sending -


i have head scratcher, have created uitableview display recorded data stored in documents directory allows me select multiple files , send them in email. works great, select listed files want send in email, click email button , email appear list of attached files. names , extensions correct.

the problem is, send email , once received attachments have dissapeared , been replaced txt files names such att00001.txt, att00002.txt , on.

can explain me why happening , how can fixed? have listed code below:

- (nsinteger)numberofsectionsintableview:(uitableview *)tableview {     // return number of sections.     return 1; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {      return [filepathsarray count]; }  -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {      uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"maincell"];     if (cell == nil) {         cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@"maincell"];     }      nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);     nsstring *documentsdirectory = [paths objectatindex:0];     nsarray *filelist = [[nsfilemanager defaultmanager] contentsofdirectoryatpath:documentsdirectory error:nil];     nspredicate *fltr = [nspredicate predicatewithformat:@"self endswith '.csv'"];     nsarray *csvfiles = [filelist filteredarrayusingpredicate:fltr];     nslog(@"contents of directory: %@", csvfiles);     filepathsarray = [[nsfilemanager defaultmanager]contentsofdirectoryatpath:documentsdirectory error:nil];     cell.textlabel.text = [csvfiles objectatindex:indexpath.row];      return cell; }  # pragma mark - deleting data row. - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath {     // return no if not want specified item editable.     return yes; }  - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {     {         nsstring *filename = [filepathsarray objectatindex:indexpath.row];         nsstring *path;         nsarray  *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);         path = [paths objectatindex:0];         path = [path stringbyappendingpathcomponent:filename];         nserror *error;         [filepathsarray removeobjectatindex:indexpath.row];          [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade];         [tableview reloaddata];         if ([[nsfilemanager defaultmanager]fileexistsatpath:path]) {             if(![[nsfilemanager defaultmanager] removeitematpath:path error:&error])             {                 nslog(@"delete file error:%@", error);             }             nslog(@"deleting file named: %@", path);         }      } }  - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {      uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath];     if (cell.accessorytype == uitableviewcellaccessorynone) {         cell.accessorytype = uitableviewcellaccessorycheckmark;         [self.selecteddata addobject:[filepathsarray objectatindex:indexpath.row]];         [[tableview indexpathsforselectedrows] count];         [self updateemailbuttontitle];         nslog(@"selecteddata %@",self.selecteddata);      } }  - (void)tableview:(uitableview *)tableview diddeselectrowatindexpath:(nsindexpath *)indexpath {     uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath];     if (cell.accessorytype == uitableviewcellaccessorycheckmark) {         cell.accessorytype = uitableviewcellaccessorynone;         [self.selecteddata removeobject:[filepathsarray objectatindex:indexpath.row]];         [self updateemailbuttontitle];         nslog(@"deselecteddata %@",self.selecteddata);     }  }  #pragma mark - email selected data  -(ibaction)emailbutton:(id)sender {     [self showemail];  }  - (void)showemail {      nsstring *emailtitle = @"your data";     nsstring *messagebody = @"attached recorded data.";      mfmailcomposeviewcontroller *mc = [[mfmailcomposeviewcontroller alloc] init];     mc.mailcomposedelegate = self;     [mc setsubject:emailtitle];     [mc setmessagebody:messagebody ishtml:no];      (nsstring *file in self.selecteddata) {          // determine file name         nsstring *filename = [self.selecteddata objectatindex:0];          // read file using nsdata          nsdata *filedata = [nsdata datawithcontentsoffile:file];          // add attachment         [mc addattachmentdata:filedata mimetype:@"text/csv" filename:filename];     }      // present mail view controller on screen     [self presentviewcontroller:mc animated:yes completion:null];  }  - (void) mailcomposecontroller:(mfmailcomposeviewcontroller *)controller didfinishwithresult:(mfmailcomposeresult)result error:(nserror *)error {     switch (result)     {         case mfmailcomposeresultcancelled:             nslog(@"mail cancelled");             break;         case mfmailcomposeresultsaved:             nslog(@"mail saved");             break;         case mfmailcomposeresultsent:             nslog(@"mail sent");             break;         case mfmailcomposeresultfailed:             nslog(@"mail sent failure: %@", [error localizeddescription]);             break;         default:             break;     }      // close mail interface     [self dismissviewcontrolleranimated:yes completion:null]; } 

any , appreciated.

edit - here finished , working code, thank helped this:

- (nsinteger)numberofsectionsintableview:(uitableview *)tableview {     // return number of sections.     return 1; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {      return [filepathsarray count]; }  -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {      uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"maincell"];     if (cell == nil) {         cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@"maincell"];     }      nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);     nsstring *documentsdirectory = [paths objectatindex:0];     nsarray *filelist = [[nsfilemanager defaultmanager] contentsofdirectoryatpath:documentsdirectory error:nil];     nspredicate *fltr = [nspredicate predicatewithformat:@"self endswith '.csv'"];     nsarray *csvfiles = [filelist filteredarrayusingpredicate:fltr];     nslog(@"contents of directory: %@", csvfiles);     filepathsarray = [[nsfilemanager defaultmanager]contentsofdirectoryatpath:documentsdirectory error:nil];     cell.textlabel.text = [csvfiles objectatindex:indexpath.row];      return cell; }  # pragma mark - deleting data row. - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath {     // return no if not want specified item editable.     return yes; }  - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {     {         nsstring *filename = [filepathsarray objectatindex:indexpath.row];         nsstring *path;         nsarray  *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);         path = [paths objectatindex:0];         path = [path stringbyappendingpathcomponent:filename];         nserror *error;         [filepathsarray removeobjectatindex:indexpath.row];          [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade];         [tableview reloaddata];         if ([[nsfilemanager defaultmanager]fileexistsatpath:path]) {             if(![[nsfilemanager defaultmanager] removeitematpath:path error:&error])             {                 nslog(@"delete file error:%@", error);             }             nslog(@"deleting file named: %@", path);         }      } }  - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {      uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath];     if (cell.accessorytype == uitableviewcellaccessorynone) {         cell.accessorytype = uitableviewcellaccessorycheckmark;         [self.selecteddata addobject:[filepathsarray objectatindex:indexpath.row]];         [[tableview indexpathsforselectedrows] count];         [self updateemailbuttontitle];         nslog(@"selecteddata %@",self.selecteddata);      } }  - (void)tableview:(uitableview *)tableview diddeselectrowatindexpath:(nsindexpath *)indexpath {     uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath];     if (cell.accessorytype == uitableviewcellaccessorycheckmark) {         cell.accessorytype = uitableviewcellaccessorynone;         [self.selecteddata removeobject:[filepathsarray objectatindex:indexpath.row]];         [self updateemailbuttontitle];         nslog(@"deselecteddata %@",self.selecteddata);     }  }   -(void)updateemailbuttontitle {      nsarray *selectedrows = [self.tableview indexpathsforselectedrows];       if (selectedrows.count == self.selecteddata.count)     {         self.emailbutton.enabled = no;         self.emailbutton.title = @"email";      } else if (selectedrows.count == 0) {         self.emailbutton.enabled = yes;         self.emailbutton.title = @"email selected data";     } }  #pragma mark - email selected data  -(ibaction)emailbutton:(id)sender {     [self showemail];  }  - (void)showemail {      nsstring *emailtitle = @"your data";     nsstring *messagebody = @"attached recorded data.";      mfmailcomposeviewcontroller *mc = [[mfmailcomposeviewcontroller alloc] init];     mc.mailcomposedelegate = self;     [mc setsubject:emailtitle];     [mc setmessagebody:messagebody ishtml:no];      (nsstring *file in self.selecteddata) {          nsstring *documentsdirectory = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0];          nsstring *csvfilepath = [documentsdirectory stringbyappendingpathcomponent:file]; // check conents of string "file" , match files located in documents directory.         nsdata *mydata = [nsdata datawithcontentsoffile:csvfilepath];          nslog(@"my nsdata %@",mydata);  //check whether nsdata nil or not          [mc addattachmentdata:mydata                                      mimetype:@"text/csv"                                      filename:file];          }          [self presentviewcontroller:mc animated:yes completion:nil];       }    - (void) mailcomposecontroller:(mfmailcomposeviewcontroller *)controller didfinishwithresult:(mfmailcomposeresult)result error:(nserror *)error {     switch (result)     {         case mfmailcomposeresultcancelled:             nslog(@"mail cancelled");             break;         case mfmailcomposeresultsaved:             nslog(@"mail saved");             break;         case mfmailcomposeresultsent:             nslog(@"mail sent");             break;         case mfmailcomposeresultfailed:             nslog(@"mail sent failure: %@", [error localizeddescription]);             break;         default:             break;     }      // close mail interface     [self dismissviewcontrolleranimated:yes completion:null]; } 

this code attached csv file me:

  nsstring *documentsdirectory = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0];      nsstring *csvfilepath = [documentsdirectory stringbyappendingpathcomponent:file]; //this checkes string "file" list of selected files can matched contents of documents directory.     nsdata *mydata = [nsdata datawithcontentsoffile:csvfilepath];      nslog(@"my nsdata %@",mydata);  //check whether nsdata nil or not      [mc addattachmentdata:mydata                                  mimetype:@"text/csv"                                  filename:file]; 

Comments