【问题标题】:Get text file from document directory ipad从文档目录 ipad 获取文本文件
【发布时间】:2012-01-27 22:13:24
【问题描述】:

全部,在我的要求中需要发送一封带有附件文本文件的邮件,该文件位于文档目录中,在这里我可以获取文档目录中的文件路径,如何从我的 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
}

【问题讨论】:

    标签: directory document ipa


    【解决方案1】:

    这是一个示例,试试这个

    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];
    

    尝试像这样在代码中更改 mimeType

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

    【讨论】:

    • 如何在 iPhone 上使用这是初学者
    • 只尝试更改 [picker addAttachmentData:data mimeType:@"text/rtf" fileName:@"log.txt"]; With [picker addAttachmentData:data mimeType:@"text/plain" fileName:@"log.txt"];
    猜你喜欢
    • 1970-01-01
    • 2014-07-09
    • 2015-11-08
    • 2016-06-23
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    相关资源
    最近更新 更多