Can't attach CSV file to iPhone Mail application

懵懂的女人 提交于 2019-12-11 06:49:42

问题


The code below works perfectly to send text (.txt) attachments via the mail application (textfile.csv needs to be modified everywhere with textfile.txt). However when I try to attach csv files it does not work at all (the mail application opens up showing the icon of the textfile.csv but when the mail arrives it has no attachment (nor the data is displayed within the message body). No difference if I change the mime type to "text/plain". I am puzzled... What am I doing wrong?? Any ideas please?

    - (NSString *)getPath {
   NSString *paths = [NSSearchPathForDirectoriesInDomains
   (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
   return [paths stringByAppendingPathComponent:@"textfile.csv"];
}

//Saves file to Documents folder
//Method writes a string to a text file
-(void) writeToTextFile{
 //get the documents directory:
 NSArray *paths = NSSearchPathForDirectoriesInDomains
 (NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];


 //make a file name to write the data to using the documents  directory:
 NSString *fileName = [NSString stringWithFormat:@"textfile.csv", 
 documentsDirectory];
 //create content
 NSString *content = @"One,Two,Three,Four,Five";

 //save content to the documents directory
 [content writeToFile:fileName 
     atomically:NO 
    encoding:NSStringEncodingConversionAllowLossy 


 error:nil]; 
}

// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet
{ 
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
 picker.mailComposeDelegate = self; 
 [picker setSubject:@"Subject"];

 // Set up recipients
 NSArray *toRecipients = [NSArray arrayWithObject:@"email@domain.com"];

 [picker setToRecipients:toRecipients];
 // Fill out the email body text
 NSString *emailBody = (@"Data is attached as a .CSV file");

 [picker setMessageBody:emailBody isHTML:NO];

 NSString *SavedDataPath = [self getPath];
 NSString *path = SavedDataPath;

 NSData *myData = [NSData dataWithContentsOfFile:path];
    [picker addAttachmentData:myData mimeType:@"text/csv" fileName:@"textfile.csv"];  

 [self presentModalViewController:picker animated:YES];
    [picker release];
}

回答1:


NSString *str_mail = @"one,two,three";
NSData *myData = [str_mail dataUsingEncoding:NSUTF8StringEncoding]; 

MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
mailController.mailComposeDelegate = self;
[mailController addAttachmentData:myData mimeType:@"text/csv" fileName:@"myfile"];
[mailController setMessageBody:@"body" isHTML:NO];
[self presentModalViewController:mailController animated:YES];



回答2:


Try keeping MIMETYPE as @"mime". .




回答3:


[mail addAttachmentData:[[[self eventController] csvDataForEvent:[self event]] dataUsingEncoding:NSUTF8StringEncoding] mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"Product.csv"]];


来源:https://stackoverflow.com/questions/3721881/cant-attach-csv-file-to-iphone-mail-application

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