下面的代碼完美地通過郵件應用程序發送文本(.txt)附件(textfile.csv需要隨處使用textfile.txt進行修改)。但是,當我嘗試附加csv文件時,它根本不起作用(郵件應用程序打開時顯示textfile.csv的圖標,但是當郵件到達時,它沒有附件(數據不顯示在郵件正文內)。區別,如果我改變,MIME類型爲「text/plain的。」我疑惑......我在做什麼錯了?任何想法嗎?無法將CSV文件附加到iPhone郵件應用程序
- (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 protected]"];
[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];
}