【问题标题】:Saving array into text file and attaching to email in swift将数组保存到文本文件并快速附加到电子邮件
【发布时间】:2016-01-26 08:26:17
【问题描述】:

我最近一直在努力将 .txt 文件创建到电子邮件中。

我有一个变量,它是我想要写入 txt 文件的字符串列表,然后将其作为附件添加到电子邮件中。

我还没有找到任何合适的文档。

期待一些伟大的意见。谢谢!

编辑--------- 我找到了这个代码示例:并且我收到以下错误。

@IBAction func createFile(sender: AnyObject) {
        let path = tmpDir.stringByAppendingPathComponent(fileName)
        let contentsOfFile = "Sample Text"
        var error: NSError?

        // Write File
        if contentsOfFile.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: &error) == false {
            if let errorMessage = error {
                println("Failed to create file")
                println("\(errorMessage)")
            }
        } else {
            println("File sample.txt created at tmp directory")
        }
    }

让路径 = tmpDir.stringByAppendingPathComponent(fileName)

我收到一条错误消息,告诉我“'String' 类型的值没有成员 URLByAppendingPathComponent'”

我该如何解决这个问题?

【问题讨论】:

标签: ios swift export-to-csv mfmailcomposer


【解决方案1】:

用于发送带有附件的邮件

import MessageUI


@IBAction func sendEmail(sender: UIButton) {

    if( MFMailComposeViewController.canSendMail() ) {
let mailComposer = MFMailComposeViewController()
            mailComposer.mailComposeDelegate = self

            //Set the subject and message of the email
            mailComposer.setSubject("Subject")
            mailComposer.setMessageBody("body text", isHTML: false)

            if let fileData = NSData(contentsOfFile: filePath) {
                mailComposer.addAttachmentData(fileData, mimeType: "text/txt", fileName: "data")
            }

            self.presentViewController(mailComposer, animated: true, completion: nil)
    }
}

基于 http://kellyegan.net/sending-files-using-swift/

从数组创建文件

let strings = ["a","b"]
let joinedString = strings.joinWithSeparator("\n")
do {
    try joinedString.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding)
} catch {

}

但是,您可以从字符串创建 NSData,而不是先将其写入文件。

//example data
let filename = "testfile"
let strings = ["a","b"]

if(MFMailComposeViewController.canSendMail()){

    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self
    mailComposer.setToRecipients([mail])
    mailComposer.setSubject("\(subject)" )
    mailComposer.setMessageBody("\(messagebody)", isHTML: false)

    let joinedString = strings.joinWithSeparator("\n")
    print(joinedString)
    if let data = (joinedString as NSString).dataUsingEncoding(NSUTF8StringEncoding){
        //Attach File
        mailComposer.addAttachmentData(data, mimeType: "text/plain", fileName: "test")
        self.presentViewController(mailComposer, animated: true, completion: nil)
    }
}

然后在结果上关闭作曲家控制器

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    controller.dismissViewControllerAnimated(true, completion: nil)
}

【讨论】:

  • 动物。谢谢大佬回复。我有这个当前代码pastebin.com/wH10RLup 它根本不显示附件,我认为我没有遗漏任何内容。请看一下。谢谢!另外,我拥有的变量已经加入了 \n 行。
  • 不幸的是,事实并非如此。没有错误。但什么都没有。
  • 我稍微更新了上面的答案,但我必须做很多工作才能真正测试这一点。你能检查'data'的值,看看它也被写了吗?
  • 我只是想知道您在执行过程中遇到的问题。能否在数据变量给定值后设置断点并检查它是否真的从字符串中获取任何数据?
  • 我尝试打印数据,但没有得到任何东西。
猜你喜欢
  • 2018-03-24
  • 2014-02-18
  • 1970-01-01
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-27
  • 2018-07-10
相关资源
最近更新 更多