【问题标题】:MFMailComposeViewController refuse to dismissMFMailComposeViewController 拒绝解雇
【发布时间】:2016-12-21 19:41:17
【问题描述】:

这让我发疯了。这个 sn-p 代码允许用户发送一封电子邮件,其中包含在应用程序中创建的图像。除了 self.dismiss(animated: true, completion: nil) 之外,一切都运行良好 - MFMailComposeViewController 不会关闭。

我使用这三个可能的问题https://stackoverflow.com/a/13217443/5274566作为我解决问题的开始,但它仍然不起作用。尽管已发送邮件或cancel 已被窃听,但控制器仍然存在。

添加了协议实现MFMailComposeViewControllerDelegate

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

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

【问题讨论】:

    标签: ios swift mfmailcomposeviewcontroller mfmailcomposer


    【解决方案1】:

    问题是您在mailOpen 函数中编写了didFinishWithResult: 委托方法,所以它永远不会被调用,并且关闭代码永远不会被执行。

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

    【讨论】:

    • 左大括号不应被包裹(参见github.com/raywenderlich/swift-style-guide#spacing)。
    • @PEEJWEEJ 没有理由仅仅为了改变花括号的位置而编辑某人的答案。不同的人喜欢不同的风格。
    • @deville:没有官方文件说应该遵循哪种风格。我更喜欢这种风格,因为它对我来说更容易匹配开括号和闭括号。
    • 谢谢!愚蠢的错误
    【解决方案2】:

    这里:

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

    您正在关闭您自己的 ViewController,而不是 MFMailComposeViewController

    应该是:

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

    【讨论】:

      猜你喜欢
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2020-09-17
      相关资源
      最近更新 更多