在我的代码中
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
。我哪里做错了?如何指定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
}
}
答案 0 :(得分:0)
试试这个
objInLoop != undefined