如何将录制的audio附加到电子邮件? 在SWIFT

我有一个应用程序,logging声音types的“WAV”,并发送电子邮件,也。 我想添加logging的文件到电子邮件作为附件? 这里是sendEmail代码

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

解决了评论的答案,为其他#程序员添加了答案:

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