Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

how to send a file from file manager in mail swift

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
        }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: swift wait until condition is true 
Swift :: declare variable in swif 
Swift :: swift comment 
Swift :: Swift Objects 
Swift :: send receive udp swift 
Swift :: Swift Add/Remove Elements From Tuple 
Swift :: swift disable modal dismiss swift 
Swift :: how to debug before app launch swift 
Swift :: Logical Operators Swift 
Swift :: auto layout issue in tableview 
Swift :: swift md5 cryptokit 
Swift :: Swift Nested Ternary Operators 
Swift :: Swift Optional Binding (if-let 
Swift :: how to print body moya swift 
Swift :: dfghbghjjmyuhjtdcfbjj 
Swift :: Swift Protocol To Calculate Area 
Swift :: called a function after some time in swift 
Swift :: Swift Closed Range 
Swift :: swift split an array into chunks 
Swift :: mp3 player with swift iOS & macOS 
Swift :: ios network request 
Swift :: how to delete from list tvos swiftui coredata 
Ruby :: how to delete a table in rails 
Ruby :: eager load polymorphic rails 
Ruby :: ruby get file folder 
Ruby :: rails logger color 
Ruby :: ruby string to int 
Ruby :: ruby if 
Ruby :: Ruby ruby-2.6.3 is present on the following stacks: heroku 16 
Ruby :: iterate over array ruby 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =