问题描述
我有我认为是一个独特的问题。我无法让我的电子邮件窗口关闭。我正在使用Xcode 8。电子邮件在第一次打开它时正确关闭,但如果我再次打开它,则不会。如果我按取消,它不会给我删除草稿的选项。如果我按发送电子邮件发送,但窗口不会关闭。
我的代码如下。第一次正确调用 mailComposeController
,但是它再也不会被调用了。有没有人对我缺少什么有任何想法?
let mail = MFMailComposeViewController()
func sendEmail(body: String,subject:String){
如果MFMailComposeViewController.canSendMail(){
mail.mailComposeDelegate = self
mail.setSubject(subject)
mail.setMessageBody( \(body),isHTML:false)
如果let data =(body as NSString).data(using:String.Encoding.utf8.rawValue){
//附加文件
mail.addAttachmentData(data,mimeType:text / plain,fileName:data.txt)
}
present(mail,animated:true)
} else {
// show failure alert
}
}
func mailComposeController(_ controller:MFMailComposeViewController,didFinishWith result:MFMailComposeResult,error:Error?){
controller.dismiss(动画:true,completion:nil)
}
您需要每次创建一个新的 MFMailComposeViewController
。在 sendEmail
中移动邮件
声明
func sendEmail(body:String,subject:String){
如果MFMailComposeViewController.canSendMail(){
//创建一个新的MFMailComposeViewController ...
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject(subject)
mail.setMessageBody(\(body),isHTML :false)
如果let data =(body as NSString).data(using:String.Encoding.utf8.rawValue){
//附加文件
mail.addAttachmentData数据,mimeType:text / plain,fileName:data.txt)
}
present(mail,animated:true)
} else {
// show failure alert
}
}
至于为什么...? / p>
I have what I believe is a unique problem. I am having trouble getting my email window to dismiss. I am using Xcode 8.
The email dismisses correctly the first time I open it, but if I open it again it won't. If I press "Cancel" it does not give me the option to "Delete Draft". If I press "Send" the email is sent, but the window does not dismiss.
My code is below. The mailComposeController
gets called correctly the first time, but it never gets called a second time. Does anyone have any ideas about what I am missing?
let mail = MFMailComposeViewController()
func sendEmail(body: String, subject: String) {
if MFMailComposeViewController.canSendMail() {
mail.mailComposeDelegate = self
mail.setSubject(subject)
mail.setMessageBody("\(body)", isHTML: false)
if let data = (body as NSString).data(using: String.Encoding.utf8.rawValue){
//Attach File
mail.addAttachmentData(data, mimeType: "text/plain", fileName: "data.txt")
}
present(mail, animated: true)
} else {
// show failure alert
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
You need to create a new MFMailComposeViewController
each time. Moving your mail
declaration inside sendEmail
works…
func sendEmail(body: String, subject: String) {
if MFMailComposeViewController.canSendMail() {
// Create a new MFMailComposeViewController…
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject(subject)
mail.setMessageBody("\(body)", isHTML: false)
if let data = (body as NSString).data(using: String.Encoding.utf8.rawValue){
//Attach File
mail.addAttachmentData(data, mimeType: "text/plain", fileName: "data.txt")
}
present(mail, animated: true)
} else {
// show failure alert
}
}
As to why…?
这篇关于MFMailComposeViewController视图不会关闭第二次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!