单击取消,保存草稿或删除草稿时无法关闭邮件视图控制器

时间:2017-12-05 12:27:21

标签: swift xcode email

我正在我的应用中实现一个邮件控制器,但我无法弄清楚为什么它在点击保存草稿或删除草稿时不会关闭。窗口卡在电子邮件屏幕上,我也无法再次点击“取消”。

@IBAction func emailButtonTapped(_ sender: UIButton) {
    guard MFMailComposeViewController.canSendMail() else {
        if !MFMailComposeViewController.canSendMail() {
            print("Can not send email")
            return
        }
        return
    }


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

    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    mailComposer.setToRecipients(["example@example.com"])
    mailComposer.setSubject("Look at this")
    mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)

    present(mailComposer, animated: true, completion: nil)
}

4 个答案:

答案 0 :(得分:4)

我成功修复了这个问题,这是一个非常愚蠢的问题! 有些代码在emailButtonTapped func:

之外
@IBAction func emailButtonTapped(_ sender: UIButton) {
    //guard MFMailComposeViewController.canSendMail() else {
        if !MFMailComposeViewController.canSendMail() {
            print("Can not send email")
            return
        }
    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    mailComposer.setToRecipients(["example@example.com"])
    mailComposer.setSubject("Look at this")
    mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)

    present(mailComposer, animated: true, completion: nil)
}

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

答案 1 :(得分:1)

你可以尝试一次:

    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {

    // Dismiss the mail compose view controller.
    controller.dismissViewControllerAnimated(true, completion: nil)
}

答案 2 :(得分:0)

Check the following Code.

@IBAction func sendEmailButtonTapped(sender: AnyObject) {
            let mailComposeViewController = configuredMailComposeViewController()
            if MFMailComposeViewController.canSendMail() {
                self.presentViewController(mailComposeViewController, animated: true, completion: nil)
            } else {
                self.showSendMailErrorAlert()
            }
    }
        func configuredMailComposeViewController() -> MFMailComposeViewController {
            let mailComposerVC = MFMailComposeViewController()
            mailComposerVC.mailComposeDelegate = self

            mailComposerVC.setToRecipients([])
            mailComposerVC.setSubject("Sending In-App Email")
            mailComposerVC.setMessageBody("Sending Email through your app in Swift", isHTML: false)

            return mailComposerVC
        }
        func showSendMailErrorAlert() {
            let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send Email.  Please check Email configuration and try again.", delegate: self, cancelButtonTitle: "OK")
            sendMailErrorAlert.show()
        }
        // MFMailComposeViewControllerDelegate Method
        func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
            controller.dismissViewControllerAnimated(true, completion: nil)
        }

答案 3 :(得分:0)

雨燕4

// MARK: - SEND EMAIL BUTTON

@IBAction func SendEmailButt(_ sender: AnyObject) {
    // This string containes standard HTML tags, you can edit them as you wish

    let messageStr = "Hello,"

    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self
    mailComposer.setSubject("Title")
    mailComposer.setMessageBody(messageStr, isHTML: true)

    if MFMailComposeViewController.canSendMail() { present(mailComposer, animated: true, completion: nil)
    } else {
        print("Your device cannot send emails. Please configure an email address into Settings -> Mail, Contacts, Calendars.")
    }
}




 // Email delegate

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        var resultMess = ""
        switch result.rawValue {
        case MFMailComposeResult.cancelled.rawValue:
            resultMess = "Mail cancelled"
        case MFMailComposeResult.saved.rawValue:
            resultMess = "Mail saved"
        case MFMailComposeResult.sent.rawValue:
            resultMess = "Thanks for contacting us!\nWe'll get back to you asap."
        case MFMailComposeResult.failed.rawValue:
            resultMess = "Something went wrong with sending Mail, try again later."
        default:break
        }

        // Show email result alert
        print(resultMess)
        dismiss(animated: true, completion: nil)
}