MFMailComposeViewController拒绝解雇

时间:2016-12-21 19:41:17

标签: ios swift mfmailcomposeviewcontroller mfmailcomposer

这让我疯了。此代码段允许用户发送包含在应用内创建的图像的电子邮件。除了public enum Orientation { Landscape = 2, Portrait = 1, Square = 3, Undefined = 0 } 之外,一切都很完美 - self.dismiss(animated: true, completion: nil)不会解散。

我使用这三个可能的问题https://stackoverflow.com/a/13217443/5274566作为解决问题的开始,但它仍然不起作用。尽管事实上控制器仍然发送,但已发送邮件或已点击MFMailComposeViewController

添加了协议实现cancel

MFMailComposeViewControllerDelegate

2 个答案:

答案 0 :(得分:2)

问题是你已经在didFinishWithResult:函数中编写了mailOpen委托方法,因此它永远不会被调用,解雇代码也不会被执行。

func mailOpen(alertAction: UIAlertAction)
{
    if MFMailComposeViewController.canSendMail()
    {
        let mailcontroller = MFMailComposeViewController()
        mailcontroller.mailComposeDelegate = self;
        mailcontroller.setSubject("Subject")
        let completeImage = newImage! as UIImage
        mailcontroller.addAttachmentData(UIImageJPEGRepresentation(completeImage, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "Image")
        mailcontroller.setMessageBody("<html><body><p>Message</p></body></html>", isHTML: true)

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

    }
    else
    {

        let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send the e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "Got it!")
        sendMailErrorAlert.show()
    }
}//end of mail

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?)
{
    self.dismiss(animated: true, completion: nil)
}

答案 1 :(得分:-2)

这里:

self.dismiss(animated: true, completion: nil)

您解雇了自己的ViewController,而不是MFMailComposeViewController

应该是:

controller.dismiss(animated: true, completion: nil)