Email a pdf file in Swift

1

Good afternoon

I have a Tab Bar Item and would like it when I clicked it to trigger a UIActivityViewController that would pay for a PDF of a link ex :( link ) and I can send it as an attachment to an email.

    
asked by anonymous 29.03.2017 / 21:57

1 answer

1

You should use the MessagesUI api to compose email to the user as follows:

import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let mailComposeVC = MFMailComposeViewController()
        mailComposeVC.mailComposeDelegate = self
        mailComposeVC.setToRecipients(["[email protected]"])
        mailComposeVC.setSubject("Assunto !!!")
        mailComposeVC.setMessageBody("Texto da mensagem", isHTML: false) // também pode mandar html

        // para mandar um arquivo
        if let data = try? Data(contentsOf: Bundle.main.url(forResource: "nomeDoArquivo", withExtension: "pdf")!) {
            print(data.count)
            mailComposeVC.addAttachmentData(data, mimeType: "application/pdf", fileName: "Any Name")
        }
        present(mailComposeVC, animated: true)
    }
}

    
30.03.2017 / 03:05