How to connect recorded audio to email? in SWIFT

I have an application that records the "wav" type of voices and sends emails. I want to add this file to my email as an attachment? here's the sendEmail code

func sendemail(email: String){
    if( MFMailComposeViewController.canSendMail() ) {
        println("Can send email.")

        let mailComposer = MFMailComposeViewController()
        mailComposer.mailComposeDelegate = self

        //Set the subject and message of the email
        mailComposer.setSubject("Voice Note")
        mailComposer.setMessageBody("my sound", isHTML: false)
        mailComposer.setToRecipients([email])
// the app doesn't go trough this IF STATEMENT 
        if let filePath1 =      NSBundle.mainBundle().pathForResource("fahad", ofType: "wav") {
            println("File path loaded.")

            if let fileData = NSData(contentsOfFile: filePath1) {
                println("File data loaded.")
                mailComposer.addAttachmentData(fileData, mimeType: "audio/wav", fileName: "fahad")
            }
        }
        self.presentViewController(mailComposer, animated: true, completion: nil)
    }
}

      

+3


source to share


1 answer


Solved the answer from the comments by adding the answer to other #Programmers:



func sendemail(email: String){
    if( MFMailComposeViewController.canSendMail() ) {
        println("Can send email.")

        let mailComposer = MFMailComposeViewController()
        mailComposer.mailComposeDelegate = self

        //Set the subject and message of the email
        mailComposer.setSubject("Voice Note")
        mailComposer.setMessageBody("my sound", isHTML: false)
        mailComposer.setToRecipients([email])

        if let docsDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as? String {
            var fileManager = NSFileManager.defaultManager()
            var filecontent = fileManager.contentsAtPath(docsDir + "/" + fileName)
            mailComposer.addAttachmentData(filecontent, mimeType: "audio/x-wav", fileName: fileName)
        }

        self.presentViewController(mailComposer, animated: true, completion: nil)
    }
}

      

+1


source







All Articles