MFMailComposeViewController appears for a second then disappears - ios

I have done the usual set up of the MFMailComposeViewController() as per Swift Guide
https://developer.apple.com/library/prerelease/ios/documentation/MessageUI/Reference/MFMailComposeViewController_class/
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.

Related

Swift email composer not working [duplicate]

I tried 2 codes from different sites to send an email from my iOS app.
When I press the Send button it calls the method mailComposeController and always returns me the log "Mail sent" as the result parameter is always MFMailComposeResultSent.value, even when I have my iPhone 5S in airplane mode.
Besides this, even when I have internet connection, I never receive the email. Already checked SPAM and other folders and waited one entire day if it was delayed but never received even trying several times.
I program with Swift and use XCode 6.4 in a Macbook Pro retina from mid-2014 with Yosemite 10.10.5.
Here the Swift code:
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if (MFMailComposeViewController.canSendMail()) {
var emailTitle = "Vea Software Feedback"
var messageBody = "Vea Software! :)"
var toRecipents = ["oscar.muntal#gmail.com"]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}else {
println("No email account found")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Email Delegate
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result.value {
case MFMailComposeResultCancelled.value:
println("Mail cancelled")
case MFMailComposeResultSaved.value:
println("Mail saved")
case MFMailComposeResultSent.value:
println("Mail sent")
case MFMailComposeResultFailed.value:
println("Mail sent failure: \(error.localizedDescription)")
default:
break
}
self.dismissViewControllerAnimated(false, completion: nil)
}
}
Thank you very much for your help in advance.
for me the following code works perfectly fine:
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if MFMailComposeViewController.canSendMail() {
let toRecipents = ["oscar.muntal#gmail.com"]
let emailTitle = "Vea Software Feedback"
let messageBody = "Vea Software! :)"
let mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setToRecipients(toRecipents)
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
presentViewController(mc, animated: true, completion: nil)
} else {
print("cannot send mails")
}
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result {
case MFMailComposeResultCancelled:
print("Mail cancelled")
case MFMailComposeResultSaved:
print("Mail saved")
case MFMailComposeResultSent:
print("Mail sent")
case MFMailComposeResultFailed:
print("Mail sent failure: \(error?.localizedDescription)")
default:
break
}
dismissViewControllerAnimated(true, completion: nil)
}
}
Thanks to Andre Slotta I solved my problem.
The problem was that I didn't have enabled the "Mail" Switch in the Settings > Mail, Contacts, Calendar > myemailaccount.
This enables to send and receive emails from the native iOS app. Instead of this I just had this enabled for the "Contacts", "Calendars" and "Notes", and use the Gmail iOS app for the Email purposes in my iPhone.
It worked with my initial code also, so it wasn't a code problem at all and everyone can try my initial code in Swift (not Swift2).
Thanks again Andre Slotta for your help!!!
Please add you email account in your mobile phone Setting!

How to close MFMailComposeViewController?

I want to send an email from my Swift application and it works well, it sends the email.
Nevertheless, after sending the email, the layout does not quits. I want to quit the layout after sending the email. I also want this behaviour when I press on Cancel, Remove draft or Save draft button.
This is the code that I have to send the email (after pressing a button):
#IBAction func btnSendEmailAction(_ sender: AnyObject) {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
composeVC.setToRecipients(["mymail#mail.com"])
composeVC.setSubject("Hello!")
composeVC.setMessageBody("Hello World!", isHTML: false)
self.present(composeVC, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result {
case MFMailComposeResult.cancelled:
controller.dismiss(animated: true, completion: nil)
break
case MFMailComposeResult.sent:
controller.dismiss(animated: true, completion: nil)
break
case MFMailComposeResult.failed:
controller.dismiss(animated: true, completion: nil)
break
default:
break
}
controller.dismiss(animated: true, completion: nil)
}
but the layout does not quits when I press on Cancel or Sent buttons.
I know that there are a lot of questions related about this problem but I looked a lot of them and this is the code I could get from a mix of some of them. Note that most of them are in Objective instead of Swift (and sometimes the methods does not exist).
Example: iPhone: How to Close MFMailComposeViewController?
Am I missing something on my code? How can I detect Remove draft and Save Draft events?
Thanks in advance!
Looks like you're using swift 3 and not using a valid delegate method. Corrected delegate method is this:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result {
case .cancelled:
break
case .saved:
break
case .sent:
break
case .failed:
break
}
dismiss(animated: true, completion: nil)
}

MFMailComposeViewControllerDelegate not working Swift 3

I have been using the mail composer in a few of my apps for awhile now and as of recent the mailComposeDelegate no longer gets call.I wasn't sure if this was something to do with the new release of Swift. So, I thought I would ask and see if anyone else is having similar issues.I can present the mail composer but it never gets dismissed due to the delegate not working.
Below is an exact copy of the code I have been using:
func launchFeedback() {
guard MFMailComposeViewController.canSendMail() else {
return
}
let emailTitle = "Feedback"
let messageBody = ""
let toRecipents = ["johnappleseed#icloud.com"]
mailComposer.mailComposeDelegate = self
mailComposer.setSubject(emailTitle)
mailComposer.setMessageBody(messageBody, isHTML: false)
mailComposer.setToRecipients(toRecipents)
self.show(mailComposer, sender: self)
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
print(error)
controller.dismiss(animated: true, completion: nil)
}
This is clearly an Xcode bug. The only way to get around this (after searching though StackOverflow life for an hour) was this:
#objc(mailComposeController:didFinishWithResult:error:)
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult,error: NSError?) {
controller.dismiss(animated: true)
}
See the #objc macro before the method implementation. Also note that the last parameter has to be NSError type instead of Error as suggested by Apple documentation (and autocompleted by Xcode)
Swift 3 no longer has unnamed first parameters by default (see this proposal), so you'll need to add an underscore to your function:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
print(error)
controller.dismiss(animated: true, completion: nil)
}
Swift 4, Xcode 9.1.
My issue was that MFMailComposeViewController was working fine but if you click cancel, dismiss, and then trying to open it one more time both cancel and send button will not fire didFinishWith delegate function.
It was happening because I've declared MFMailComposeViewController as lazy variable and solution was to create new instance of MFMailComposeViewController every time you want to open it.
Problem:
lazy var mailComposeViewController: MFMailComposeViewController = {
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.mailComposeDelegate = self
mailComposeViewController.setToRecipients(["example#test.test"])
mailComposeViewController.setSubject("subject")
mailComposeViewController.setMessageBody("test body", isHTML: false)
return mailComposeViewController
}()
Solution:
func createMailComposeViewController() -> MFMailComposeViewController {
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.mailComposeDelegate = self
mailComposeViewController.setToRecipients(["example#test.test"])
mailComposeViewController.setSubject("subject")
mailComposeViewController.setMessageBody("test body", isHTML: false)
return mailComposeViewController
}
After wasting 2 good hours, i came to the conclusion that as of Xcode 8.3. MFMailComposeViewController does not work on a mixed swift/objc code base. It pops odd compile errors, which first i thought were due to my stupidity, but no.
This is so frustrating apple. Most of us old timers do have tons of code on obj-c, so a pure swift scenario is close to impossible. So as i move classes to swift i have to deal with extra pain as well.
Adding
#import <MessageUI/MessageUI.h>
to the AppName-Bridging-Header.h did the job !

how to dismiss mail view controller after tapping send or cancel button

while sending mail, after tapping send or cancel button view controller stays there and app stalls.
//swift 2.2 ; xcode 7.3.1 ;
if( MFMailComposeViewController.canSendMail() ) {
print("Can send email.")
}
var subjectText = "Verification"
var toReceipients = ["notorious.roman#gmail.com"]
// var msgBody = "Verified"
var mc:MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(subjectText)
mc.setMessageBody("Verified", isHTML: false)
mc.setToRecipients(toReceipients)
self.presentViewController(mc, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Swift 4.0 Update.
Swift 5.0 Update.
Allow me to add something to the discussion...
In Swift 4 and 5 the delegate method slightly changed; As it's posted by you now, won't do any effect and won't get called. It happened to me, drove me crazy!
The Xcode warning suggest three fixes but first two could be misleading. It's just a tiny fix...
Here's the delegate method fixed for Swift 3, 4 and 5:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
Still, Victor's answer should be the correct/accepted one.
Enjoy!
I think #rmaddy answer your question in his comment, nevertheless I going to explain you what's happening. You're trying to dismiss the UIViewController that presents the MFMailComposeViewController not the MFMailComposeViewController.
As Apple specify in his documentation:
The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController:didFinishWithResult:error: method of its delegate. Your implementation of that method must dismiss the view controller explicitly.
So you need to set the method in this way:
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// Dismiss the mail compose view controller.
controller.dismissViewControllerAnimated(true, completion: nil)
}
I hope this help you.
is had an Switch Statement that controls it for me:
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
}
self.dismiss(animated: true, completion: nil)
}

Tried to send email from in-app and doesn't work - Swift (iOS)

I tried 2 codes from different sites to send an email from my iOS app.
When I press the Send button it calls the method mailComposeController and always returns me the log "Mail sent" as the result parameter is always MFMailComposeResultSent.value, even when I have my iPhone 5S in airplane mode.
Besides this, even when I have internet connection, I never receive the email. Already checked SPAM and other folders and waited one entire day if it was delayed but never received even trying several times.
I program with Swift and use XCode 6.4 in a Macbook Pro retina from mid-2014 with Yosemite 10.10.5.
Here the Swift code:
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if (MFMailComposeViewController.canSendMail()) {
var emailTitle = "Vea Software Feedback"
var messageBody = "Vea Software! :)"
var toRecipents = ["oscar.muntal#gmail.com"]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}else {
println("No email account found")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Email Delegate
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result.value {
case MFMailComposeResultCancelled.value:
println("Mail cancelled")
case MFMailComposeResultSaved.value:
println("Mail saved")
case MFMailComposeResultSent.value:
println("Mail sent")
case MFMailComposeResultFailed.value:
println("Mail sent failure: \(error.localizedDescription)")
default:
break
}
self.dismissViewControllerAnimated(false, completion: nil)
}
}
Thank you very much for your help in advance.
for me the following code works perfectly fine:
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if MFMailComposeViewController.canSendMail() {
let toRecipents = ["oscar.muntal#gmail.com"]
let emailTitle = "Vea Software Feedback"
let messageBody = "Vea Software! :)"
let mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setToRecipients(toRecipents)
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
presentViewController(mc, animated: true, completion: nil)
} else {
print("cannot send mails")
}
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result {
case MFMailComposeResultCancelled:
print("Mail cancelled")
case MFMailComposeResultSaved:
print("Mail saved")
case MFMailComposeResultSent:
print("Mail sent")
case MFMailComposeResultFailed:
print("Mail sent failure: \(error?.localizedDescription)")
default:
break
}
dismissViewControllerAnimated(true, completion: nil)
}
}
Thanks to Andre Slotta I solved my problem.
The problem was that I didn't have enabled the "Mail" Switch in the Settings > Mail, Contacts, Calendar > myemailaccount.
This enables to send and receive emails from the native iOS app. Instead of this I just had this enabled for the "Contacts", "Calendars" and "Notes", and use the Gmail iOS app for the Email purposes in my iPhone.
It worked with my initial code also, so it wasn't a code problem at all and everyone can try my initial code in Swift (not Swift2).
Thanks again Andre Slotta for your help!!!
Please add you email account in your mobile phone Setting!

Resources