在PdfKit swift中使用pdf的mailComposeViewController

时间:2018-02-16 10:11:32

标签: swift mfmailcomposeviewcontroller pdfkit

我在Swift with PdfKit

中有一个pdf文件的代码

但是电子邮件没有打开应用程序我就收到了任何错误

 let mailComposeViewController = MFMailComposeViewController()
        if let lastPathComponent = pdfDocument?.documentURL?.lastPathComponent,
            let documentAttributes = pdfDocument?.documentAttributes,
            let attachmentData = pdfDocument?.dataRepresentation() {
            if let title = documentAttributes["Title"] as? String {
                mailComposeViewController.setSubject(title)
            }
            mailComposeViewController.addAttachmentData(attachmentData, mimeType: "application/pdf", fileName: lastPathComponent)
        }

1 个答案:

答案 0 :(得分:0)

    var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    var documentsDirectory: String = paths[0]
    var finalPath: String = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("myPdf.pdf").absoluteString
    print("\(finalPath)")
    var error: Error? = nil
    var myData = Data(base64Encoded: pdfContent, options: [])
    var url = URL.fileURL(withPath: finalPath)
    if error != nil {
        print("Fail: \(error?.localizedDescription)")
    }
    var mailComposer = MFMailComposeViewController()

    if MFMailComposeViewController.canSendMail() {

        mailComposer.mailComposeDelegate = self
        mailComposer.addAttachmentData(myData!, mimeType: "application/pdf", fileName: "myPdf.pdf")
        self.present(mailComposer, animated: true, completion: nil)
    }