2012-05-09 30 views
5

我已經創建了一個csv文件,我也將它附加到MFMailComposer,它向我展示了我的郵件編輯器,但是當我將它發送給用戶電子郵件時,它不會顯示我在電子郵件中附加的csv文件。我已經使用此代碼來創建CSV文件並在其中添加數據。如何在iPhone SDK中的MFMailComposer中創建和附加CSV文件?

 NSMutableString *mainString=[[NSMutableString alloc]initWithString:@""]; 
     //NSMutableArray *section = [[NSMutableArray alloc] init]; 
     for(int i = 0;i<[NameArray count];i++) 
     { 
      NSString *string=[indexArray objectAtIndex:i]; 
      string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]; 
      [mainString appendFormat:@"\"%@\"",string]; 


      string=[NameArray objectAtIndex:i]; 
      string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]; 
      [mainString appendFormat:@",\"%@\"",string]; 

      string=[typearray objectAtIndex:i]; 
      string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]; 
      [mainString appendFormat:@",\"%@\"",string]; 

      [mainString appendFormat:@",\"%@\"",string]; 
      [mainString appendFormat:@"\n"]; 

     } 

     NSLog(@"getdatafor csv:%@",mainString); 

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
     NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"history.csv"]; 
//  filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     NSData* settingsData; 
     settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding]; 

     NSError *error; 
     [settingsData writeToFile:filePath atomically:YES]; 
//   NSLog(@"writeok"); 
     NSData *mediaData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMapped error:&error]; 

     NSLog(@"Length:%d Error:%@",[mediaData length],[error localizedDescription]); 

這裏上述代碼工作良好我越來越[mediaData長度]我從這裏附加CSV文件。

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

    // Attach an image to the email 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"history" ofType:@"csv"]; 
    NSData *myData = [NSData dataWithContentsOfFile:path]; 

    // Fill out the email body text 
    NSString *emailBody = @"history"; 
    [picker setMessageBody:emailBody isHTML:NO]; 
    [picker addAttachmentData:myData mimeType:@"text/cvs" fileName:@"history"]; 

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

上述代碼也正常工作。它顯示我附加的CSV文件,但是當我通過電子郵件發送郵件時,接收者沒有獲得附加的CSV文件。這段代碼有什麼問題?爲什麼接收者沒有得到附件?

+0

嘿有人可以幫我嗎? –

+0

嘿,任何機構請給我想法在我們的郵件編輯器窗口中附加csv文件。 –

+0

picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setToRecipients:[NSArray arrayWithObjects:@「abc.xyz.com」,nil]]; [picker setSubject:@「iMoneyManager - CSV Exported」]; [picker setMessageBody:@「」isHTML:NO]; [picker addAttachmentData:mediaData mimeType:@「text/csv」fileName:@「MoneyManager」]; picker.navigationBar.tintColor = [UIColor blackColor]; [self presentModalViewController:picker animated:YES]; –

回答

0

我已經解決了這個問題,在MFMailComposeViewController中附加文件和其他媒體屬性。

0
NSData *data=[[arr componentsJoinedByString:@","] writeToFile:@"Bhavesh.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL]; 
[mail addAttachmentData:data mimeType:@"text/csv" fileName:@"Bhavesh.csv"]; 
3
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; 
mailer.mailComposeDelegate = self; 
[mailer setSubject:@"CSV File"]; 

NSData *myData = [text dataUsingEncoding:NSUTF8StringEncoding]; 

[mailer addAttachmentData:myData mimeType:@"text/cvs" fileName:@"FileName"]; 

[self presentModalViewController:mailer animated:YES]; 

凡 '文本' 是一個字符串。

相關問題