我有一个应用程序记录“wav”的声音类型并发送电子邮件。我想将录制的文件作为附件添加到电子邮件中?这里是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)
}
}
答案 0 :(得分:1)
解决了评论中的答案,为其他#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)
}
}