【问题标题】:Email finished but the send email window does not close? [duplicate]电子邮件已完成但发送电子邮件窗口未关闭? [复制]
【发布时间】:2017-05-09 13:23:54
【问题描述】:

我的问题是我对 emailwindow 进行了编程,它确实会发送电子邮件,但是当它们被发送或我想关闭窗口时,什么都没有发生。

这是我的代码:

import Foundation
import UIKit
import MessageUI


class ContactViewController: UIViewController, MFMailComposeViewControllerDelegate, UIAlertViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

let mail = MFMailComposeViewController()


    @IBAction func email(_ sender: Any) {

        if !MFMailComposeViewController.canSendMail() {
            let warnung = UIAlertController(title: "Email konnte nicht gesendet werden", message: "Dein Gerät unterstützt leider keine Email-Funktion.", preferredStyle: .alert)
            let action1 = UIAlertAction(title: "OK", style: .default, handler: nil)
            warnung.addAction(action1)
            self.present(warnung, animated: true, completion: nil)
            return

            } else {

                mail.mailComposeDelegate = self
                mail.setToRecipients(["team@example.com"])
                mail.setSubject("Message to you")
                mail.setMessageBody("Hello,\n", isHTML: false)

                present(mail, animated: true, completion: nil)

            func mailComposeController(_ controller: MFMailComposeViewController,
                                       didFinishWithResult result: MFMailComposeResult, error: NSError?) {
                mail.dismiss(animated: true, completion: nil)
                print("Yes!")
                }


            }
        }
}

这是邮件窗口的屏幕截图: Just click on this link!

【问题讨论】:

  • 为什么委托方法在email(_sender:)方法内部,在else测试中?
  • 因为我认为当它在 else 方法中时效果更好
  • 请注意,您的方法 didFinishWithResult 已在您的 IBAction 电子邮件方法中声明。您需要将该方法移出该 IBAction 方法。它必须是您的视图控制器的实例方法

标签: ios swift mfmailcomposeviewcontroller


【解决方案1】:
@IBAction func email(_ sender: Any) {

    if !MFMailComposeViewController.canSendMail() {
        let warnung = UIAlertController(title: "Email konnte nicht gesendet werden", message: "Dein Gerät unterstützt leider keine Email-Funktion.", preferredStyle: .alert)
        let action1 = UIAlertAction(title: "OK", style: .default, handler: nil)
        warnung.addAction(action1)
        self.present(warnung, animated: true, completion: nil)
        return

        } else {

            mail.mailComposeDelegate = self
            mail.setToRecipients(["team@example.com"])
            mail.setSubject("Message to you")
            mail.setMessageBody("Hello,\n", isHTML: false)

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

func mailComposeController(_ controller: MFMailComposeViewController,
                                   didFinishWithResult result: MFMailComposeResult, error: NSError?) {
            mail.dismiss(animated: true, completion: nil)
            print("Yes!")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 2018-11-27
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    相关资源
    最近更新 更多