【问题标题】:Save string as a file attachment with custom name IOS将字符串保存为具有自定义名称 IOS 的文件附件
【发布时间】:2013-12-16 07:20:11
【问题描述】:

我对 IoS 比较陌生,在使用邮件附件时遇到问题。下面是我的代码。

    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.txt",
                          documentsDirectory];
    //create content - four lines of text
    NSString *content = @"One\nTwo\nThree\nF\nFive";
    //save content to the documents directory
    [content writeToFile:fileName
              atomically:NO
                encoding:NSStringEncodingConversionAllowLossy
                   error:nil];
    mailComposer = [[MFMailComposeViewController alloc]init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:@"Test mail"];
    NSData *myData = [NSData dataWithContentsOfFile:fileName];
    [mailComposer addAttachmentData:myData mimeType:@"text/plain" fileName:fileName];
    [mailComposer setMessageBody:@"Testing message for the test mail" isHTML:NO];
    [self presentModalViewController:mailComposer animated:YES];

这很好用,我收到了邮件附件。但是,如果我将文件名更改为

NSString *fileName = [NSString stringWithFormat:@"%@",
                              @"Sample.txt"];

在撰写邮件中显示附件,但发送时不会有任何附件。任何人都可以建议如何完成同样的事情

【问题讨论】:

    标签: iphone ios7 email-attachments mfmailcomposeviewcontroller


    【解决方案1】:

    是的,它不会起作用,因为您必须提供访问您尝试附加的文件的完整路径,就像在您的示例中一样:

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

    这是 textfile.txt 的完整路径以及文档目录路径。 这就是你没有正确获得依恋的原因。您必须提供您尝试附加的文件的完整路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 2021-09-08
      • 2021-12-06
      • 2021-12-30
      • 2015-01-10
      相关资源
      最近更新 更多