swift pdf file not attaching to an email - swift

I have the below code which is suppose to attach a file named "file.pdf" which was created and placed into the temp directory as such
pdfFileUrl = "\(NSTemporaryDirectory())file.pdf"
But the email does not have my attachment as I am expecting it to
#IBAction func sendEmail(sender: AnyObject) {
createReportPDF()
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["dpreston10#gmail.com"])
mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
if let filePath = NSBundle.mainBundle().pathForResource(pdfFileUrl, ofType: "pdf") {
print("File path loaded.")
if let fileData = NSData(contentsOfFile: filePath) {
print("File data loaded.")
mail.addAttachmentData(fileData, mimeType: "application/pdf", fileName: "file.pdf")
}
}
presentViewController(mail, animated: true, completion: nil)
} else {
// show failure alert
}
}

Because your file is put into the user's temporary folder, I do not think it will be accessible from the a NSBundle method. That said, if you are writing the NSData to disk in the temporary folder and then directly emailing it, you may want to consider just passing that variable (pdfData) to the mail controller like:
mail.addAttachmentData(pdfData, mimeType: "application/pdf", fileName: "file.pdf")

Related

I want to attach a file in Xcode to an email

I wrote the code as below
if MFMailComposeViewController.canSendMail()
{
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
let fileName = "sampleText"
let url = "textFiles/sampleText.txt"
do{
let data = try Data(contentsOf: url)
mail.addAttachmentData(data, mimeType: "txt", fileName: fileName)
present(mail, animated: true)
}catch {
print("error ...")
}
} else {
}
I get the following error
Cannot convert value of type 'String' to expected argument type 'URL'
The file structure of my Xcode looks like the image below
How do I clear the error?
You need to load it like
guard let url = Bundle.main.url(forResource: "sampleText", withExtension: "txt") else { return }
Note : folders with yellow colors textFiles are fake while ones with blue colors are true

Create Zip file from an array of data (coreData Images) and share via the .zip file via Email

I have an (n) of data (UIImage JPEG) inside my CoreData.
let imageData: [Data]...
I have already this two frameworks/ Pods: Zip and ZIPFoundation
I have a few Question about that:
I need to create a temp URL for each of my imageData?
If yes, I have to add tempURL.appendingPathExtension("jpg") to each temp URLs before or after call data.write(to: tempURL) ?
After that, I have an Array of URLs, so I just need to create a Zip File and share it. But it doesn't work, I get a .zip - .cpgz Loop on my Mac.
private func createURLsFrom(imageData: [ImageData]?) {
var urlArray = [URL]()
imageData?.forEach { imData in
if let data = imData.imageData,
let tempURL = NSURL.fileURL(withPathComponents: [NSTemporaryDirectory(), NSUUID().uuidString])?.appendingPathExtension("jpg") {
do {
try data.write(to: tempURL)
urlArray.append(tempURL)
} catch {...}
}
}
self.createZipFile(urlArray: urlArray)
}
private func createZipFile(urlArray: [URL]) {
if let zipURL = try? Zip.quickZipFiles(urlArray, fileName: "ZIP_Test1") {
self.sendEmailWith(dataURL: zipURL)
} else {...}
}
private func sendEmailWith(dataURL: URL) {
if MFMailComposeViewController.canSendMail() {
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setSubject("setSubject")
mailComposer.setMessageBody("setMessageBody", isHTML: false)
mailComposer.addAttachmentData(dataURL.dataRepresentation, mimeType: "application/zip", fileName: ("ZIP_Test1.zip"))
self.present(mailComposer, animated: true, completion: nil)
}
}
What am I doing wrong :(
It's a bit lengthy, and––disclaimer––untested. Let me know if it works or if you have any questions.
Create a temp directory for all the files:
func createTempDirectory() -> URL? {
if let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let dir = documentDirectory.appendingPathComponent("temp-dir-\(UUID().uuidString)")
do {
try FileManager.default.createDirectory(atPath: dir.path, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error.localizedDescription)
}
return dir
} else {
return nil
}
}
Save all the images to the temp directory:
func saveImages(data: [Data]) -> URL? {
guard let directory = createTempDirectory() else { return nil }
do {
for (i, imageData) in data.enumerated() {
try imageData.write(to: directory.appendingPathComponent("image\(i).jpg"))
}
return directory
} catch {
return nil
}
}
Get the URL for the zipped file. This is an optional in case an error occurred along the way. Also, done on a background thread because it could take a bit of time, and you don't want to block the main thread.
func zipImages(data: [Data], completion: #escaping ((URL?) -> ())) {
DispatchQueue.main.async {
guard let directory = saveImages(data: data) else {
completion(nil)
return
}
do {
let zipFilePath = try Zip.quickZipFiles([directory], fileName: "archive-\(UUID().uuidString)")
completion(zipFilePath)
} catch {
completion(nil)
}
}
}
After you send the file, you'll probably want to delete the temp directory so your app size doesn't start growing.
After a lot of debugging finally, I found the problem.
First of all Daniels answer was correct too.
But the main issue was my sendEmailWith function, "application/zip" is not working!
So I added UIActivityViewController with the directory URL and it's work.
Now I can share my Zip file via Mail and Airdrop!
Move the
self.createZipFile(urlArray: urlArray)
to after the forEach loop.

Swift pdf attachment in Mail

In my code
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
}
I am creating pdf and would like to send through mail. Mail works. But the pdf is not attached. If I use simulator the directory is /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
if I use iPhone, the save directory is:
/var/mobile/Containers/Data/Application/7374E0A1-3E49-494D-B554-1D9C761FC7C1/Documents/Test - Monday, 25 December 2017.pdf and the console message is 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 when I try to attach mail. Where am I doing wrong? How to specify the same location for the iPhone to attache the created pdf file? Please help.
EDIT:
Below code works and attaching the created pdf with 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 = 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
}
}
Swift 4.0
try this
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)

UIDocumentInteractionController file attachment error

so I'm working on exporting a .txt file from Xcode, but whenever I try to export it, say through email, it creates an email with no attachments and an empty message. When I try to export it to iCloud Drive, a white screen comes up for a second, then leaves. When I check iCloud Drive, the file isn't there. What is wrong here?
let message = testLabel.text
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory as NSString
}
let filePath = getDocumentsDirectory().appendingPathComponent("matrixFile.txt")
do{
try message?.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
}catch let error as NSError{
testLabel.text = String(describing: error)
}
let documentInteractionController = UIDocumentInteractionController()
documentInteractionController.url = NSURL.fileURL(withPath: filePath)
documentInteractionController.uti = "public.data, public.content"
documentInteractionController.name = "matrixFile"
documentInteractionController.presentOptionsMenu(from: view.frame, in: view, animated: true)
The problem is you are declaring your document interaction controller as a constant within the method that calls it, like:
func someButton() {
let controller = UIDocumentInteractionController...
}
It will not work like that. The interaction controller needs to be an instance variable of the class that presents it, and it should be reinitialized every time you present it with a new url, like so:
var controller: UIDocumentInteractionController!
func someButton() {
controller = UIDocumentInteractionController(url: someURL)
controller.presentOptionsMenu...
}
Why? I have no idea, but file a bug report if you think this is absurd.
Your UTI is wrong. For a text file you should use "public.text". But there is a constant for this already - kUTTypePlainText. You just need to import MobileCoreServices to use it.
import MobileCoreServices
then:
documentInteractionController.uti = kUTTypePlainText as String
You should also use the proper initializer when creating the controller:
let documentInteractionController = UIDocumentInteractionController(url: URL(fileURLWithPath: filePath))
documentInteractionController.uti = kUTTypePlainText as String
documentInteractionController.name = "matrixFile"
documentInteractionController.presentOptionsMenu(from: view.frame, in: view, animated: true)
And I would only create and show the controller if the text isn't nil and you successfully write the file:
if let message = testLabel.text {
let filePath = getDocumentsDirectory().appendingPathComponent("matrixFile.txt")
do {
try message.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
let documentInteractionController = UIDocumentInteractionController(url: URL(fileURLWithPath: filePath))
documentInteractionController.uti = kUTTypePlainText as String
documentInteractionController.name = "matrixFile"
documentInteractionController.presentOptionsMenu(from: view.frame, in: view, animated: true)
} catch let error as NSError {
testLabel.text = String(describing: error)
}
}

Send mail with file attachment

I search solution to send a mail with attachment.
I have this code but the file is not attached...
if let url = URL(string: "mailto:\(email)?subject=report&body=see_attachment&attachment=/Users/myname/Desktop/report.txt") {
NSWorkspace.shared().open(url)
}
I have see it maybe work with MessageUI, but I can't import this framework I don't know why. I get this error message : No such module 'MessageUI'
I checked in General > Linked Frameworks and Libraries, but there are not MessageUI...
Anyone have a solution to add file in mail?
Thanks
It seems that attachment in mailto: URLs are not supported on macOS (not always at least...details seems sketchy dependent on where you look on the internet :))
What you can use instead I found out from this blog post, is an instance of NSSharingService documented here
Here is an example demonstrating how to use it.
And in your case you could do something like:
let email = "your email here"
let path = "/Users/myname/Desktop/report.txt"
let fileURL = URL(fileURLWithPath: path)
let sharingService = NSSharingService(named: NSSharingServiceNameComposeEmail)
sharingService?.recipients = [email] //could be more than one
sharingService?.subject = "subject"
let items: [Any] = ["see attachment", fileURL] //the interesting part, here you add body text as well as URL for the document you'd like to share
sharingService?.perform(withItems: items)
Update
So #Spire mentioned in a comment below that this won't attach a file.
It seems there is a gotcha to be aware of.
For this to work you need to look into your App Capabilities.
You can either:
disable App Sandbox
enable read access for the folders from where you would like to fetch content.
I've attached a couple of screenshots.
Here is how this looks if I have disabled App Sandbox under Capabilities
And here is an image where I have enabled App Sandbox and allowed my app to read content in my Downloads folder
If I do the above, I can access my file called document.txt, located in my Downloads folder, using this URL
let path = "/Users/thatsme/Downloads/document.txt"
let fileURL = URL(fileURLWithPath: path)
And attach that to a mail
Hope that helps you.
import MessageUI
class ViewController: UIViewController,MFMailComposeViewControllerDelegate {
func sendMail() {
if( MFMailComposeViewController.canSendMail()){
print("Can send email.")
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
//Set to recipients
mailComposer.setToRecipients(["yakupad#yandex.com"])
//Set the subject
mailComposer.setSubject("email with document pdf")
//set mail body
mailComposer.setMessageBody("This is what they sound like.", isHTML: true)
let pathPDF = "\(NSTemporaryDirectory())contract.pdf"
if let fileData = NSData(contentsOfFile: pathPDF)
{
print("File data loaded.")
mailComposer.addAttachmentData(fileData as Data, mimeType: "application/pdf", fileName: "contract.pdf")
}
//this will compose and present mail to user
self.present(mailComposer, animated: true, completion: nil)
}
else
{
print("email is not supported")
}
func mailComposeController(_ didFinishWithcontroller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
{
self.dismiss(animated: true, completion: nil)
}
}
First of all you should import import MessageUI. For this add framework to the project.
Example:
After investigate MFMailComposeViewControllerDelegate for knowing when you end sending email.
Example of the creating of the email:
if( MFMailComposeViewController.canSendMail() ) {
println("Can send email.")
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
//Set the subject and message of the email
mailComposer.setSubject("Have you heard a swift?")
mailComposer.setMessageBody("This is what they sound like.", isHTML: false)
if let filePath = NSBundle.mainBundle().pathForResource("swifts", ofType: "wav") {
println("File path loaded.")
if let fileData = NSData(contentsOfFile: filePath) {
println("File data loaded.")
mailComposer.addAttachmentData(fileData, mimeType: "audio/wav", fileName: "swifts")
}
}
self.presentViewController(mailComposer, animated: true, completion: nil)
}
Working example is presented by this link.