我一直在研究这款应用。我无法查看要关闭的电子邮件的视图。我该怎么办?
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
@IBOutlet weak var Label: UITextField!
@IBAction func SendOrder(_ sender: Any) {
let recipients = ["EmailHere"]
let title = "My order is:"
let message = Label.text
let mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setToRecipients(recipients)
mc.setSubject(title)
mc.setMessageBody(message!, isHTML: false)
self.present(mc, animated: true, completion: nil)
我已尝试过Delegate方法,但它提出了这个: 我该怎么办?
答案 0 :(得分:1)
尝试
@IBAction func btnEmail(_ sender: AnyObject) {
self.openEmail()
}
func openEmail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject("Your subject Name")
mail.setMessageBody("Contact Us", isHTML: false)
mail.setToRecipients(["test@gmail.com"])
present(mail, animated: true, completion: { _ in })
}
else {
print("This device cannot send email")
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result {
case .sent:
print("You sent the email.")
break
case .saved:
print("You saved a draft of this email")
break
case .cancelled:
print("You cancelled sending this email.")
break
case .failed:
print("Mail failed: An error occurred when trying to compose this email")
break
}
dismiss(animated: true, completion: { _ in })
}