在Swift Email中附加.txt文件

时间:2015-10-26 04:15:20

标签: ios swift email-attachments mfmailcomposeviewcontroller mfmailcomposer

我无法将文本文件附加到电子邮件中。没有任何东西显示为附件。

我已经盯着这个太久lol请帮帮我。谢谢!

编辑:经过一番研究后,我发现我无法编写theList变量,因为它是一个数组。它应先减少,然后再写。

let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    //Set Recipient
    mailComposer.setToRecipients([email])
    //Set Subject
    mailComposer.setSubject("VanScan for " + "\(TechID.text!)" )
    //Set Body Message
    mailComposer.setMessageBody("The inventory for " + "\(TechID.text!)" + " is attached", isHTML: false)

    if let filePath = NSBundle.mainBundle().pathForResource("\(filename)", ofType: "txt") {
        //Does not print. Nil?
        print("File path loaded.")

        if let fileData = NSData(contentsOfFile: filePath) {
            //Does not print. Nil?
            print("File data loaded.")
            mailComposer.addAttachmentData(fileData, mimeType: "text/plain", fileName: "\(filename)")
        }
    }
    self.presentViewController(mailComposer, animated: true, completion: nil)
    }

0 个答案:

没有答案