0
我已成功將PDF文檔附加到電子郵件,如下所示。將PDF附加到Swift中的電子郵件中,但PDF未解碼
@IBAction func emailScript(_ sender: Any) {
print ("Now the value of pdfPath is \(SelectedPDF)")
let pdfURL = URL(string:SelectedPDF)
print ("The value of pdfURL is \(pdfURL)")
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
mail.setToRecipients(["Enter one or more emails here"])
mail.mailComposeDelegate = self // Make sure to set this property to self, so that the controller can be dismissed!
//Set the subject
mail.setSubject("email with document pdf")
if let fileData = NSData(contentsOf: pdfURL!) {
print ("File data loaded.")
print (fileData)
mail.addAttachmentData(fileData as Data, mimeType: "application/pdf", fileName: "GST")
}
present(mail, animated: true, completion: nil)
}
else {
print ("Error")
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
我確定fileData不是零。數據流顯示在控制檯中。附件顯示爲PDF。但是當我從我的應用程序發送電子郵件時,PDF不可讀。在我的iPad中,我看到一個帶有標題欄的灰色屏幕。在我的Mac上,我收到一個錯誤消息,說明PDF未解碼。
PDF來自我的Dropbox,它的URL來自Parse服務器應用程序。
PDF鏈接,它在瀏覽器的工作原理,是https://www.dropbox.com/s/rt28egrkyc7uhc7/GSTPurim5777.pdf?dl=0
我在做什麼錯?
謝謝
禮
您正在將數據文件而不是PDF文件附加到消息中。 –
@ElTomato這是附加文件的唯一方法。最有可能的問題是,URL實際上不會導致PDF文件。 – rmaddy
這是PDF的URL。它在瀏覽器中工作。 https://www.dropbox.com/s/rt28egrkyc7uhc7/GSTPurim5777.pdf?dl=0 – user3140521