Share a pdf file in Swift by email

Asked

Viewed 74 times

1

Good afternoon

I have a Tab Bar Item and would like when I click on it to trigger a Uiactivityviewcontroller that would pay a PDF of an ex link:(http://www.meusite/boleto.pdf) and I can send it as an email attachment.

  • the most that Voce can do is compose a new message with the Attached file and the user sends it

  • actually the file has to be on the device. If you want to send the link in the body of the email can also

  • I managed to upload this pdf in a webview but now I don’t know how to share in an email adding this file, you would have an example code?

  • @Leodabus send the link in the body of the email I got but do not know how to send the file, would have an example code?

  • to view pdf uses Quicklook instead of webview

  • https://developer.apple.com/reference/messageui/mfmailcomposeviewcontroller/1616885-addattachmentdata

  • ta using Mfmail Compose View Controller?

  • if you can’t talk to me. mime type for pdf is "application/pdf"

  • I’m using uiactivityindicatorview but when it opens the option to compose the email the most I can put there is a link

  • @Leodabus has no way to do this without using API, natively??

  • More native than that ? What’s the problem with adding Messageui import?

  • @Leodabus no problem, I just wanted to finish without adding any API, I had some problems with the Alamofire and I was a little reluctant to use

  • This Framework is from Apple itself

  • And it’s so old it supports iOS 3.0+

  • @Good Leodabus I’ll try with her then, that code there works I already having the PDF in view?

  • easier still to use the PDF date instead of reading from the file url that is within the Bundle

  • how so got the pdf in view? Voce meant webview? In the web view I never tried. If Voce do not post your code it is difficult to help

  • Explain better where you have the pdf you want to send

Show 13 more comments

1 answer

1


You must use the Messagesui api to compose the email pro 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)
    }
}

inserir a descrição da imagem aqui

  • Leo my code for now is this

  • asyncTask(parameters: String, id: String) { Dispatchqueue.global(Qos: Dispatchqos.userInitiated.qosClass). async {`
 
 DispatchQueue.main.async {
 let boletoURL = URL(string: "http://www.meusite.com/01_2017.pdf")
 print(boletoURL!)
 let boletoURLRequest = URLRequest(url: boletoURL!)
 self.webView.isUserInteractionEnabled = true self.webView.loadRequest(boletoURLRequest) } } }

  • You only need to download your pdf first using Urlsession dataTask(with URL:) . no need to show in webview.

  • But the intention is to show on screen, and if the user wants to share to his or someone else’s email having this option

  • No need does not mean that Voce can not. Until better Voce download and display the local file.

  • What do you prefer to save the pdf or just leave it in memory ? There is no way to save what you have in the web view without having to render its contents and a pdf can have several pages. If you want to download to memory has size limit. but as it is to send by email 20MB has no problem

  • I can’t get this pdf that’s on the webview and attach it to an email?

  • yes take the URL and download

  • That’s where I don’t know what else to do, I need to change this code that I have when I load the url in webview?

  • That’s why I said it’s best to download before displaying either on the web view or Quicklook. Quicklook so displays local files so as I said low before

  • Maybe you can even get the pdf in the app cache but I never tried

  • you have some code that does all this and already attach?

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.