2009-11-07 55 views
7

我遇到了通過MFMailComposeViewController發送csv附件的問題。 有時他們會過得很好,但對於其他用戶而言,他們不是以附件形式出現,而是作爲電子郵件中的文本內聯(與< br/>而不是線返回)。這很奇怪。有人知道我做錯了什麼? 這裏是我的代碼片段:MFMailComposeViewController csv附件未連接,但顯示內聯而不是

MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init]; 
mailComposeViewController.mailComposeDelegate = self; 

NSString *csv = @"foo,bar,blah,hello"; 
NSData *csvData = [csv dataUsingEncoding:NSUTF8StringEncoding]; 
[mailComposeViewController addAttachmentData:csvData mimeType:@"text/csv" fileName:@"testing.csv"]; 

[mailComposeViewController setSubject:@"testing sending csv attachment"]; 
[mailComposeViewController setMessageBody:@"csv file should be attached" isHTML:NO]; 
[self presentModalViewController:mailComposeViewController animated:YES]; 

回答

0

我相信setMessageBody:isHTML:第二個參數必須是YES的附件不顯示嵌入式。

0

即使您將isHTML參數設置爲YES,如果郵件正文可以如此表示,那麼您的郵件正文可以作爲純文本/文本發送。某些電子郵件客戶端(Outlook)並不總能正確識別純文本/短信中的附件。

在我的情況下,在消息體中添加鏈接有所幫助。使用HTML標籤將文本格式化爲粗體也可以。整蠱!

在iPod上測試1G 3.1.3。

10
-(IBAction)btnPressed:(id)sender { 
    NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *docDir = [arrayPaths objectAtIndex:0]; 
    NSString *Path = [docDir stringByAppendingString:@"/CSVFile.csv"]; 
    NSData *csvData = [NSData dataWithContentsOfFile:Path]; 

    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; 
    controller.mailComposeDelegate = self; 

    [controller setSubject:@"For csv file..."]; 
    [controller setMessageBody:@"...csv file is hear.." isHTML:NO]; 
    [controller addAttachmentData:csvData mimeType:@"text/csv" fileName:@"CSVFile.csv"]; 
    [self presentModalViewController:controller animated:YES]; 
    [controller release]; 
} 
+1

其很好的感謝 – jal 2011-02-22 11:15:38

0

這可能不是這裏的情況,但有一點需要注意的是:

[NSString dataUsingEncoding:] 

返回一個有效的,但空NSData對象,如果轉換爲指定的編碼是不可能的。最好使用完整版:

[NSString dataUsingEncoding: s allowLossyConversion: YES] 

或者檢查返回數據的長度。看起來零長度的數據附件在郵件處理的某個地方被修剪。

2

嗨我把創建CSV文件的示例代碼,並附上郵件,但確保你必須添加MessageUI.Framework並導入其相關的標題「MessageUI/MessageUI.h」 「MessageUI/MFMailComposeViewController.h」 「MFMailComposeViewControllerDelegate」 ......我希望這WL爲他人

- (void)viewDidLoad { 

arrCsv=[[NSArray alloc]initWithObjects:@"Hello",@"Hi",@"traun",@"fine",nil]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains 

(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *fileName = [NSString stringWithFormat:@"%@/try.csv", documentsDirectory]; 

[[arrCsv componentsJoinedByString:@","] writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL]; 

} 



-(ibAction)btnMail { 

NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString *docDir = [arrayPaths objectAtIndex:0]; 
NSString *Path = [docDir stringByAppendingString:@"/CSVFile.csv"]; 
NSData *csvData = [NSData dataWithContentsOfFile:Path]; 
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; 
controller.mailComposeDelegate = self; 
[controller setSubject:@"For csv file..."]; 
[controller setMessageBody:@"...csv file is hear.." isHTML:NO]; 
[controller addAttachmentData:csvData mimeType:@"text/csv" fileName:@"CSVFile.csv"]; 
[self presentModalViewController:controller animated:YES]; 
[controller release]; 

} 


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ message.hidden = NO; 
switch (result) 
{ 
    case MFMailComposeResultCancelled: 
     message.text = @"Result: canceled"; 
     break; 
    case MFMailComposeResultSaved: 
     message.text = @"Result: saved"; 
     break; 
    case MFMailComposeResultSent: 
     message.text = @"Result: sent"; 
     break; 
    case MFMailComposeResultFailed: 
     message.text = @"Result: failed"; 
     break; 
    default: 
     message.text = @"Result: not sent"; 
     break; 
} 
[self dismissModalViewControllerAnimated:YES]; 
} 
1

有用的設置MIME類型爲「應用程序/八位字節流」,而應該做的伎倆刪除內嵌附件(我還是命名的延長我的文件即PDF)

+0

隨着iOS 8,這是工作的解決方案 爲了我。 – lifjoy 2015-07-09 21:01:46

+0

沒有幫助我在iOS 8上使用jpeg附件。 – shelll 2015-08-19 12:32:14