I have done the usual set up of the MFMailComposeViewController() as per Swift Guide
but when I run this then the email appears for a split second, disappears and I get the error message "MailCompositionService quit unexpectedly".
here is the full code
import Foundation
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
@IBAction func showEmail(sender: AnyObject) {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["address@example.com"])
composeVC.setSubject("Hello!")
composeVC.setMessageBody("Hello from California!", isHTML: false)
// Present the view controller modally.
self.presentViewController(composeVC, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result.rawValue {
case MFMailComposeResultCancelled.rawValue:
print("Mail cancelled")
case MFMailComposeResultSaved.rawValue:
print("Mail saved")
case MFMailComposeResultSent.rawValue:
print("Mail sent")
case MFMailComposeResultFailed.rawValue:
print("Mail sent failure: \(error!.localizedDescription)")
default:
break
}
controller.dismissViewControllerAnimated(true, completion: nil)
}}
This is a known bug in the xcode simulator. It should work fine on your device.
Your code is correct. The MFMailComposeViewController
component can't be tested in the iOS simulator
only in the device
.
If you look this Thread in Apple Developer Forums
the problem has a ticket in Apple Bug Report
but still without any fix.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.