MFMailViewCompose不会关闭

时间:2016-09-26 09:19:28

标签: ios iphone swift xcode ios10

我正在构建一个简单的iOS10应用程序。我已经构建了一个页面,用户可以在其中添加主题行和电子邮件正文。点击发送邮件按钮时,MFMailViewCompose正常启动,如果我点击发送电子邮件发送。但是,MFMailViewCompose在发送或取消时不会关闭。

这是代码

import UIKit
import MessageUI

class EnglishEmailHelpdeskVC: UIViewController, MFMailComposeViewControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

//Helpdesk Email Subject Line and Body.
@IBOutlet var EmailHelpdeskSubjectLine: UITextField!
@IBOutlet var EmailHelpdeskBody: UITextView!

var MailControllerCompose:MFMailComposeViewController!

//Helpdesk Send Email Button.
@IBAction func EmailHelpdeskSendButton(_ sender: AnyObject) {
let HDSubjectLine = EmailHelpdeskSubjectLine.text
let HDBody = EmailHelpdeskBody.text

MailControllerCompose = MFMailComposeViewController()
MailControllerCompose.mailComposeDelegate = self
MailControllerCompose.setToRecipients(["example@example.com"])
MailControllerCompose.setSubject(HDSubjectLine!)
MailControllerCompose.setMessageBody(HDBody!, isHTML: false)

self.present(MailControllerCompose, animated: true, completion: nil)

}

@nonobjc func MFMailComposeViewContoller(controller: MFMailComposeViewController,didFinishWithResult result:MFMailComposeResult, error: NSError?){
MailControllerCompose.dismiss(animated: true, completion: nil)
}
}

请帮忙!

1 个答案:

答案 0 :(得分:0)

经过与这个问题的激烈斗争后,最终有效的是:

public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Swift.Error?) {
    controller.dismiss(animated: true, completion: nil)
}

最后一个参数中的关键部分是Swift.Error。如果没有Swift限定符,Error会解析为UIViewController.Error,这与我们尝试覆盖的方法不匹配。我怀疑你选择的NSError因为同样的原因而失败。

(值得注意的是,我的错误Error类型显然来自PromiseKit,而不是像我原先想的那样来自UIKit。)