我的iOS发送电子邮件应用程序出现问题。我创建了一个联系表单,用户可以键入姓名,电话号码,电子邮件地址等,最后还有一个发送按钮。当按下发送按钮时,应用程序崩溃。我没有收到任何错误消息来说明我的应用崩溃的原因。
这是我下面的代码。我缺少任何可能导致我的应用崩溃的东西吗?
import UIKit
import MessageUI
class SendEmail: UIViewController, MFMailComposeViewControllerDelegate {
@IBOutlet var nameField: UITextField!
@IBOutlet var emailAddressfield: UITextField!
@IBOutlet var phoneNumberfield: UITextField!
@IBOutlet var requestfield: UITextField!
@IBAction func Send(sender: Any) {
if !MFMailComposeViewController.canSendMail() {
print("Email Account Not Found")
} else {
let mc = MFMailComposeViewController()
mc.mailComposeDelegate = self
//let toRecipients = ["st.johnamechurch@att.net"]
mc.setToRecipients(["st.johnamechurch@att.net"])
mc.setSubject("Prayer Request: ")
mc.setMessageBody("Name: \(nameField.text!) \n\nEmail: \(emailAddressfield.text!) \n\nPhoneNumber: \(phoneNumberfield.text!) \n\nPrayerRequest: \(requestfield.text!)", isHTML: false)
self.present(mc, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result.rawValue {
case MFMailComposeResult.cancelled.rawValue:
print("Cancelled")
case MFMailComposeResult.failed.rawValue:
print("failed")
case MFMailComposeResult.saved.rawValue:
print("saved")
case MFMailComposeResult.sent.rawValue:
print("sent")
default:
break
}
controller.dismiss(animated: true, completion: nil)
}
func dismissKeyboard(_ sender: AnyObject) {
self.resignFirstResponder()
}
}
}