繁体   English   中英

邮件中的Swift pdf附件

[英]Swift pdf attachment in Mail

用我的代码

class DocViewController: UIViewController,UITextViewDelegate, MFMailComposeViewControllerDelegate{
    var result:String!
    override func viewDidLoad() {
        super.viewDidLoad()
        result = "/Test - " + dateToday!
        func getPDFFileName(_ name: String) -> String {
            let newPDFName = "\(name).pdf"
            let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
            let documentsDirectory = paths[0]
            let pdfFileName: String = (documentsDirectory as String).appending(newPDFName);
            print(pdfFileName)
            return pdfFileName
        }
        @IBAction func sendMail(_ sender: UIBarButtonItem) {
            let mailComposeViewController = configuredMailComposeViewController()
            if MFMailComposeViewController.canSendMail() {
                self.present(mailComposeViewController, animated: true, completion: nil)
            } else {
                self.showSendMailErrorAlert()
            }
        }
        func configuredMailComposeViewController() -> MFMailComposeViewController {
            let mailComposer:MFMailComposeViewController = MFMailComposeViewController()
            mailComposer.mailComposeDelegate = self
            let recipients = ["a@a.com"]
            //Set the subject and message of the email
            mailComposer.setToRecipients(recipients)
            mailComposer.setSubject("Test")
            mailComposer.setMessageBody("Send Saved PDF File", isHTML: false)

            if let filePath = Bundle.main.path(forResource: getPDFFileName(result), ofType: "pdf") {
                print("File path loaded.")

                if let fileData = NSData(contentsOfFile: filePath) {
                    print("File data loaded.")
                    mailComposer.addAttachmentData(fileData as Data, mimeType: "application/pdf", fileName: "pdf")
                }
                present(mailComposer, animated: true, completion: nil)
            }
            return mailComposer
        }

我正在创建pdf,并希望通过邮件发送。 邮件有效。 但PDF未随附。 如果我使用模拟器,则目录为/Users/xxxxxx/Library/Developer/CoreSimulator/Devices/91BD76E3-7BD6-49E9-87E7-63C87BE980EF/data/Containers/Data/Application/16350B51-6898-45AA-BEF3-F0B0E4FF7556/Documents/Test - Monday, 25 December 2017.pdf

如果我使用iPhone,则保存目录为: /var/mobile/Containers/Data/Application/7374E0A1-3E49-494D-B554-1D9C761FC7C1/Documents/Test - Monday, 25 December 2017.pdf ,控制台消息为2017-12-25 20:36:45.344149+0530 Karma[7690:1157301] [App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction在尝试附加邮件时, 2017-12-25 20:36:45.344149+0530 Karma[7690:1157301] [App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction2017-12-25 20:36:45.344149+0530 Karma[7690:1157301] [App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction 我在哪里做错了? 如何为iPhone指定相同的位置来附加创建的pdf文件? 请帮忙。

编辑:以下代码有效,并将创建的pdf附加到邮件中:

class DocViewController: UIViewController,UITextViewDelegate, MFMailComposeViewControllerDelegate{
    var result:String!
    override func viewDidLoad() {
        super.viewDidLoad()
        result = "/Test - " + dateToday!
        func getPDFFileName(_ name: String) -> String {
            let newPDFName = "\(name).pdf"
            let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
            let documentsDirectory = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as! String
            let pdfFileName: String = (documentsDirectory as String).appending(newPDFName);
            print(pdfFileName)
            return pdfFileName
        }
        @IBAction func sendMail(_ sender: UIBarButtonItem) {
            let mailComposeViewController = configuredMailComposeViewController()
            if MFMailComposeViewController.canSendMail() {
                present(mailComposeViewController, animated: true, completion: nil)
            } else {
                self.showSendMailErrorAlert()
            }
        }
        func configuredMailComposeViewController() -> MFMailComposeViewController {
            let mailComposer:MFMailComposeViewController = MFMailComposeViewController()
            mailComposer.mailComposeDelegate = self
            let recipients = ["a@a.com"]
            //Set the subject and message of the email
            mailComposer.setToRecipients(recipients)
            mailComposer.setSubject("Test")
            mailComposer.setMessageBody("Send Saved PDF File", isHTML: false)
            let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as! String

            let filePath = getPDFFileName(result)
                print("File path loaded.")

                if let fileData = NSData(contentsOfFile: filePath) {
                    print("File data loaded.")
                    mailComposer.addAttachmentData(fileData as Data, mimeType: "application/pdf", fileName: result)

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

迅捷4.0

尝试这个

    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    //Set to recipients
    mailComposer.setToRecipients(["your email address heres"])

    //Set the subject
    mailComposer.setSubject("email with document pdf")
    //set mail body
    mailComposer.setMessageBody("This is what they sound like.", isHTML: true)
    if let filePath = Bundle.main.path(forResource: "All_about_tax", ofType: "pdf")
    {
        print("File path loaded.")
        if let fileData = NSData(contentsOfFile: filePath)
        {
            print("File data loaded.")
            mailComposer.addAttachmentData(fileData, mimeType: "application/pdf", fileName: "All_about_tax.pdf")
        }
    }
    //this will compose and present mail to user
    self.present(mailComposer, animated: true, completion: nil)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM