Can't sent csv file attachment with mail

余生长醉 提交于 2019-12-04 16:51:25

In my situation the email was sent fine from my iPhone, but there was no attachment when sending from my wife's iPhone. Turns out that her default account was set to her Yahoo account and that was not allowing attachments. I simply switched it to her mobile Me account and the attachment was made. I use gmail so I never had a problem.

in addition, I had tried switching the MIME type from text/csv to text/plain and that did not help. It worked either way on my iPhone and not at all on my wife's.

Hope this helps!

I have tried to attach .csv file with "text/plain" & I got success. I think you should try the same. best of luck.

I ran into the same problems and I fixed the problem by doing a really hard look at two functions that I wrote:

One defines the filename from a .plist file, the other one actually sets the location of the file (including it's path AND filename).

Using the right function to retrieve the filename in the MFMailViewController fixed the issue:

    -(NSString *)dataFilePath { //this gets the ,plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

return [documentsDirectory stringByAppendingPathComponent:kFilename];

//----------------------------

    -(NSString *)fileName { // this defines the fileName from the values in the .plist

NSString *patientDetails = [self dataFilePath];
NSArray *dataArray = [[NSArray alloc] initWithContentsOfFile:patientDetails];

NSString *firstName = [dataArray objectAtIndex:0];
NSString *lastName = [dataArray objectAtIndex:1];

NSString *fileName = [firstName stringByAppendingString:lastName];

return [fileName stringByAppendingString:@".csv"];

//-------------------------

     -(NSString *)setFileLocation {  //this puts name and folder together.  

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *fileName = [self fileName];   
NSString *fullFilePath = [NSString stringWithFormat:@"%@/%@", docDirectory,fileName];
  // NSLog(@"Filepath is : %@",fullFilePath);
return fullFilePath;

}

//-------------------------

The last one is the function you want to call in your MFMailComposeViewController:

     ...
NSData *csvData = [NSData dataWithContentsOfFile:[self setFileLocation]]; 
    [picker addAttachmentData:csvData mimeType:@"text/csv" fileName:fileName];
     ...

fileName, of course, is an NSString, retrieved by calling the fileName function: NSString *fileName = [self fileName];

Hope this helps!

If your code looks okay I'll suspect that that myData didn't get loaded with data is nil. Put in an NSLog([myData description]) or use the debugger to check that myData is not nil.

I'm out of ideas. If I was stuck in this situation I would create a stand alone project with the example code you provided and see if I could get it working. Usually in these situations the code you are focusing on is probably correct and error is coming from somewhere else.

Also:

  • Check to see if the path variable is nil because that will cause the attachment to not appear.
  • Make sure there is a icon in message body the mail compose view. When you successfully add an attachment you should see the icon representation in the view.
  • And you probably want set your fileName to rainy.png, i.e. [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy.png"].

Good luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!