2012-01-27 123 views
0

所有,在我的需求中需要發送附加文本文件的郵件,它位於文檔目錄中,在這裏我可以獲取文檔目錄中的文件路徑,我如何附加從我的ipad文件發送。從文檔目錄中獲取文本文件ipad

這裏是我試圖

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

if ([MFMailComposeViewController canSendMail]){ 
    // Create and show composer 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSLog(@"directry path %@",paths); 
    NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"Logs/log-858743.txt"]; 

    NSLog(@"directry full path %@",fullPath); 
    NSData *data = [NSData dataWithContentsOfFile: fullPath]; 
    NSLog(@"Data value %@",data); 

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath]; 
    if (fileExists) { 
     NSLog(@"file is there"); 
    }else{ 
     NSLog(@"file is not there"); 
    } 

    [picker setSubject:@"Backup"]; 
    [picker addAttachmentData:data mimeType:@"text/rtf" fileName:@"log.txt"]; 
    [picker setMessageBody:@"testing." isHTML:NO]; 
    [self presentModalViewController:picker animated:YES]; 
    [picker release]; } 
else{ 
    // Show some error message here 
} 

回答

0

這裏的樣本代碼,試試這個

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

//Imposto l'oggetto 
[mViewController setSubject:@"Invio Copia Commissione"]; 

//Preparo l' A: Devo spezzare gli indirizzi E-mail 
NSArray *toRecipients = [NSArray arrayWithArray:[Globals.AEmail componentsSeparatedByString:@";"]]; 
[mViewController setToRecipients:toRecipients]; 


//Preparo il Cc: Devo spezzare gli indirizzi E-mail 
if ([Config.EmailAgg length] != 0) { 
    NSArray *CcRecipients = [NSArray arrayWithArray:[Config.EmailAgg componentsSeparatedByString:@";"]]; 
    [mViewController setCcRecipients:CcRecipients]; 
} 

//Setto il corpo dell'E-mail 
[mViewController setMessageBody:Globals.TestoEmail isHTML:NO]; 


//A questo punto visto che ho fatto 30 salvo il testo anche come file allegato dell'E-mail 
[Globals.TestoEmail writeToFile:[NSString stringWithFormat:@"%@/CopiaCommissione.txt", Globals.DocumentsPath] atomically:TRUE encoding:NSUTF8StringEncoding error:nil]; 
NSData *Allegato = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/CopiaCommissione.txt", Globals.DocumentsPath]]; 
[mViewController addAttachmentData:Allegato mimeType:@"text/plain" fileName:@"CopiaCommissione.txt"]; 


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

嘗試改變mime類型在你的代碼是這樣

[picker addAttachmentData:data mimeType:@"text/plain" fileName:@"log.txt"]; 
+0

如何使用這是我的初學者iphone – ganesh 2012-01-27 11:19:18

+0

只嘗試改變[picker addAttachmentData:data mimeType:@「text/rtf」fileName:@「l og.txt「];使用[picker addAttachmentData:data mimeType:@「text/plain」fileName:@「log.txt」]; – Tommaso 2012-01-27 13:37:37