MFMailComposeViewController refuse to dismiss - ios

This is driving me nuts. This snippet of code lets the user send an email with an image which is created within the app. Everything works perfectly except the self.dismiss(animated: true, completion: nil) - the MFMailComposeViewController won't dismiss.
I used these three possibly problems https://stackoverflow.com/a/13217443/5274566 as my start to solve the problem, but it still won't work. The controller stays despite the fact than an mail has been sent or cancel has been tapped.
The protocol implementation MFMailComposeViewControllerDelegate is added.
func mailOpen(alertAction: UIAlertAction) {
if MFMailComposeViewController.canSendMail() {
let mailcontroller = MFMailComposeViewController()
mailcontroller.mailComposeDelegate = self;
mailcontroller.setSubject("Subject")
let completeImage = newImage! as UIImage
mailcontroller.addAttachmentData(UIImageJPEGRepresentation(completeImage, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "Image")
mailcontroller.setMessageBody("<html><body><p>Message</p></body></html>", isHTML: true)
self.present(mailcontroller, animated: true, completion: nil)
} else {
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send the e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "Got it!")
sendMailErrorAlert.show()
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
self.dismiss(animated: true, completion: nil)
}
}//end of mail

Issue is you have written the didFinishWithResult: delegate method inside the mailOpen function, so it will never be called and the dismissing code won't be executed ever.
func mailOpen(alertAction: UIAlertAction)
{
if MFMailComposeViewController.canSendMail()
{
let mailcontroller = MFMailComposeViewController()
mailcontroller.mailComposeDelegate = self;
mailcontroller.setSubject("Subject")
let completeImage = newImage! as UIImage
mailcontroller.addAttachmentData(UIImageJPEGRepresentation(completeImage, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "Image")
mailcontroller.setMessageBody("<html><body><p>Message</p></body></html>", isHTML: true)
self.present(mailcontroller, animated: true, completion: nil)
}
else
{
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send the e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "Got it!")
sendMailErrorAlert.show()
}
}//end of mail
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?)
{
self.dismiss(animated: true, completion: nil)
}

here:
self.dismiss(animated: true, completion: nil)
you're dismissing your own ViewController, rather than the MFMailComposeViewController
It should be:
controller.dismiss(animated: true, completion: nil)

Related

On ios Cancel and send button on the native mail not working.User got blocked

Below is the function for the native mail app open , Nothing happed when cancel button clicked and Send button on native mail app is sending the mail but user also got blocked.User didn't get any action respone.
#IBAction func openNativeEmail(_ sender: AnyObject){
if MFMailComposeViewController.canSendMail() {
debugPrint("can send mail")
let mailVC = MFMailComposeViewController()
mailVC.mailComposeDelegate = self
mailVC.setToRecipients(["pawanline#gmail.com"])
mailVC.setSubject("Testing mail App features")
mailVC.setCcRecipients(["pawan.kumar#iic.ac.in"])
mailVC.setMessageBody("Hi,just testing ", isHTML: false)
present(mailVC, animated: true, completion: nil)
} else {
print("Unable to send the mail")
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
You missing a closing bracket:
#IBAction func openNativeEmail(_ sender: AnyObject){
if MFMailComposeViewController.canSendMail() {
debugPrint("can send mail")
let mailVC = MFMailComposeViewController()
mailVC.mailComposeDelegate = self
mailVC.setToRecipients(["pawanline#gmail.com"])
mailVC.setSubject("Testing mail App features")
mailVC.setCcRecipients(["pawan.kumar#iic.ac.in"])
mailVC.setMessageBody("Hi,just testing ", isHTML: false)
present(mailVC, animated: true, completion: nil)
} else {
print("Unable to send the mail")
}
} // <----- THIS WAS MISSING
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}

Can't get app to dismiss the mail compose view controller & return to first screen

I'm using the Swift book to try to learn to code. I added the delegate method to dismiss the view, but it's not working. What am I missing here?
#IBAction func emailButtonTapped(_ sender: UIButton) {
if !MFMailComposeViewController.canSendMail() {
print("Can not send mail")
return
}
guard MFMailComposeViewController.canSendMail() else { return }
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients(["example#example.com"])
mailComposer.setSubject("Look at this")
mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)
present(mailComposer, animated: true, completion: nil)
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true, completion: nil)
}
}
You just need to declare the delegate function outside of the IBAction:
#IBAction func emailButtonTapped(_ sender: UIButton) {
if !MFMailComposeViewController.canSendMail() {
print("Can not send mail")
return
}
guard MFMailComposeViewController.canSendMail() else { return }
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients(["example#example.com"])
mailComposer.setSubject("Look at this")
mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)
present(mailComposer, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true, completion: nil)
}

How do I send email on swift?

I have followed peoples code on sending an email on swift and the problem is on my phone, the send button is greyed out. What do I do? Here is my code and a picture of the problem on my phone.
The Problem On My Phone, The Send Button Is Greyed Out.
import UIKit
import MessageUI
class SendEmailViewController: UIViewController, MFMailComposeViewControllerDelegate {
#IBAction func sendEmail(_ sender: Any)
{
let mailComposeViewController = configureMailController()
if MFMailComposeViewController.canSendMail() {
self.present(mailComposeViewController, animated: true, completion: nil)
} else {
showMailError()
}
}
func configureMailController() -> MFMailComposeViewController
{
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["abc#gmail.com"])
mailComposerVC.setSubject("Gamifiction")
mailComposerVC.setMessageBody("Hey, Check Out My Game", isHTML: false)
return mailComposerVC }
func showMailError()
{
let sendMailErrorAlert = UIAlertController(title: "Could not send email", message: "Your device could not send email", preferredStyle: .alert)
let dismiss = UIAlertAction(title: "Ok", style: .default, handler: nil)
sendMailErrorAlert.addAction(dismiss)
self.present(sendMailErrorAlert, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}

Email finished but the send email window does not close? [duplicate]

This question already has answers here:
Swift 3 How to display a confirmation screen based on MFMailComposeResult email screen
(2 answers)
Closed 5 years ago.
my problem is that I programmed emailwindow and it does send emails, but when they are send or I want to close the window there´s nothing that happens.
Thats my code:
import Foundation
import UIKit
import MessageUI
class ContactViewController: UIViewController, MFMailComposeViewControllerDelegate, UIAlertViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
let mail = MFMailComposeViewController()
#IBAction func email(_ sender: Any) {
if !MFMailComposeViewController.canSendMail() {
let warnung = UIAlertController(title: "Email konnte nicht gesendet werden", message: "Dein Gerät unterstützt leider keine Email-Funktion.", preferredStyle: .alert)
let action1 = UIAlertAction(title: "OK", style: .default, handler: nil)
warnung.addAction(action1)
self.present(warnung, animated: true, completion: nil)
return
} else {
mail.mailComposeDelegate = self
mail.setToRecipients(["team#example.com"])
mail.setSubject("Message to you")
mail.setMessageBody("Hello,\n", isHTML: false)
present(mail, animated: true, completion: nil)
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
mail.dismiss(animated: true, completion: nil)
print("Yes!")
}
}
}
}
Here´s a screenshot of the mail window :
Just click on this link!
#IBAction func email(_ sender: Any) {
if !MFMailComposeViewController.canSendMail() {
let warnung = UIAlertController(title: "Email konnte nicht gesendet werden", message: "Dein Gerät unterstützt leider keine Email-Funktion.", preferredStyle: .alert)
let action1 = UIAlertAction(title: "OK", style: .default, handler: nil)
warnung.addAction(action1)
self.present(warnung, animated: true, completion: nil)
return
} else {
mail.mailComposeDelegate = self
mail.setToRecipients(["team#example.com"])
mail.setSubject("Message to you")
mail.setMessageBody("Hello,\n", isHTML: false)
present(mail, animated: true, completion: nil)
}
}
}
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
mail.dismiss(animated: true, completion: nil)
print("Yes!")
}

button from app opens email but won't close the window and return to app

I have a button in my app which opens up an email to be sent to me, when this button is pressed the email app on iPhone opens up and when sent is pressed the email is sent however the window doesn't close and then return to my app. Also when i press cancel it gives the option to save/delete draft but again doesn't close the window and return to my app. I have attached the email code below.
#IBAction func SendMessage(sender: AnyObject) {
var mail: MFMailComposeViewController!
let toRecipients = ["usalim76#gmail.com"]
let subject = "Enquiry"
let body = "Your body text"
mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(toRecipients)
mail.setSubject(subject)
mail.setMessageBody(body, isHTML: true)
presentViewController(mail, animated: true, completion: nil)
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
}
looks like you forgot to implement the MFMailComposeViewControllerDelegate, add this:
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
Managed to fix it!
#IBAction func SendMessage(sender: AnyObject) {
let mailComposeViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
self.presentViewController(mailComposeViewController, animated: true, completion: nil)
} else {
self.showSendMailErrorAlert()
}
}
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property
mailComposerVC.setToRecipients(["someone#somewhere.com"])
mailComposerVC.setSubject("Sending you an in-app e-mail...")
mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)
return mailComposerVC
}
func showSendMailErrorAlert() {
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
sendMailErrorAlert.show()
}
// MARK: MFMailComposeViewControllerDelegate Method
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}

Resources