1
我想實現MessageUI和MFMailComposeViewController以在我的應用程序中發送電子郵件。這一切似乎工作正常,除非我開始過程後實際上不能離開郵件客戶端。如果我點擊發送,它會發送,然後什麼也不做(不返回到應用程序)。如果我點擊取消,它不會刪除草稿和套件(或再次返回到應用程序)。卡住在iOS應用程序中的新郵件電子郵件客戶端
import UIKit
import MessageUI
class ContactFormViewController: UITableViewController, MFMailComposeViewControllerDelegate {
...
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.item{
case 0:
followOnEmail()
default:
break
}
}
private func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.dismiss(animated: true, completion: nil)
}
func followOnEmail(){
if !MFMailComposeViewController.canSendMail(){
print("Mail services are not available")
return
}
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["[email protected]"])
composeVC.setSubject("")
composeVC.setMessageBody("Please enter your message below.", isHTML: false)
if MFMailComposeViewController.canSendMail(){
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
}
else{
self.showSendMailErrorAlert()
}
}
...
func showSendMailErrorAlert(){
let alertController = UIAlertController(title: "Could Not Send Email", message: "Your device could not send email. Please check e-mail configuration and try again.", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Close", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
}