Monday 15 June 2015

ios - Attached file to email--file not there upon receipt -



ios - Attached file to email--file not there upon receipt -

i want attach .csv file email user can send it. @ point, know file created when i'm working in simulator (i can find , open it), , appears attached email created. however, since can't send email sim, that's far there.

deployed on ipad, indications file created , attached email (correctly titled file icon included in body of email). can email it, no file attached when email received.

here's code i'm using create , write file:

-(nsstring *)datafilepath { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nslog(@"datafilepath created"); homecoming [documentsdirectory stringbyappendingpathcomponent:@"wmdgstats.csv"]; nslog(@"file path: %@", documentsdirectory); } -(void) createcsvfile { nslog(@"top of createcsvfile"); // [[nsfilemanager defaultmanager] removeitematpath:pngfilepath error:&error]; if (![[nsfilemanager defaultmanager] fileexistsatpath:[self datafilepath]]) { [[nsfilemanager defaultmanager] createfileatpath: [self datafilepath] contents:nil attributes:nil]; nslog(@"file created"); } ...yada, yada info creation nsfilehandle *handle; handle = [nsfilehandle filehandleforwritingatpath: [self datafilepath] ]; [handle truncatefileatoffset:0]; [handle writedata:[fullstring datausingencoding:nsutf8stringencoding]]; [self mailcsv]; }

and here's way attach email:

-(void) mailcsv { if ([mfmailcomposeviewcontroller cansendmail]) { mfmailcomposeviewcontroller *mail = [[mfmailcomposeviewcontroller alloc] init]; mail.mailcomposedelegate = self; [mail setsubject:@"subject"]; [mail setmessagebody:@"message body" ishtml:no]; [mail settorecipients:@[@"testingemail@example.com"]]; nsdata *wmdgdata = [nsdata datawithcontentsoffile:@"wmdgstats.csv"]; [mail addattachmentdata:wmdgdata mimetype:@"text.csv" filename:@"wmdgstats.csv"]; [self presentviewcontroller:mail animated:yes completion:null]; } else { nslog(@"this device cannot send email"); } }

any ideas?

thanks looking!

edit:

per rmaddy's kind guidance in comment below, supplied total file path file:

nsdata *wmdgdata = [nsdata datawithcontentsoffile:[documentsdirectory stringbyappendingpathcomponent:@"wmdgstats.csv"]];

and works perfectly--file created, attached, mailed , received!

thanks rmaddy!!!

the next line:

nsdata *wmdgdata = [nsdata datawithcontentsoffile:@"wmdgstats.csv"];

is returning nil because need specify proper path file. since saving file in documents folder, need specify total path.

ios email attachment

No comments:

Post a Comment