MFMailComposeViewControllerDelegate not being called - ios

I realize this question has been inexactly asked around, but I haven't been able to find an answer to my problem.
I have a UITableViewController with static cells. One of these cells is meant to open a Mail Composer view, and dismiss it through the delegate after the user sends or cancels the email. My problem is that the delegate method is not being called. Here is my code:
class SideMenuTableViewController: UITableViewController, MFMailComposeViewControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
mailCVP.delegate = self
mailCVP = configureMailComposeVC()
if MFMailComposeViewController.canSendMail() {
self.presentViewController(mailCVP, animated: true, completion: nil)
} else { //..// }
}
func configureMailComposeVC() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.setToRecipients(["momentosdetora#gmail.com"])
mailComposerVC.setSubject("Contacto Momentos de Tora")
return mailComposerVC
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
Can anybody spot anything I might be doing wrong?
Thanks.

MFMailComposeViewController is a subclass of UINavigationController, which already has a delegate property to handle navigation changes.
MFMailComposeViewController has another property called mailComposeDelegate, which is the property you are looking for.
Also, you should create the controller before setting the delegate.

Make sure you use
controller.mailComposeDelegate = self
Not this one
controller.delegate = self

mailCVP.delegate = self
mailCVP = configureMailComposeVC()
This code sets the delegate but then creates a new instance, which doesn't have a delegate...
Note that there is also no point in creating the VC instance if MFMailComposeViewController.canSendMail returns false.

First of all use
mailCVP.mailComposeDelegate = self
instead of
mailCVP.delegate = self
Moreover, in case of Swift 3, delegate method is somehow updated which is:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?){
controller.dismiss(animated: true, completion: nil)
}

Related

Cannot dismiss MFMailComposeViewController that I called from SKScene

I am trying to send an email from within my game app. In one of my SKScenes I have a sprite when you press it, it calls FeedbackVC().sendEmail(). This opens up the email viewController, but it does not dismiss properly. Here is my entire FeedbackVC class. I used the function getTopMostViewController because without it I was getting the error "Warning: Attempt to present on whose view is not in the window hierarchy!". My code will successfully open the MFMailComposeViewController with the prefilled fields and if I press the send button it actually will send to the email to my email, but it won't close and if I try to cancel the email it won't close either. Why won't my viewController close so it will continue back to my game after the email is sent or canceled?
import Foundation
import MessageUI
class FeedbackVC: UINavigationController, MFMailComposeViewControllerDelegate {
func getTopMostViewController() -> UIViewController? {
var topMostViewController = UIApplication.shared.keyWindow?.rootViewController
while let presentedViewController = topMostViewController?.presentedViewController {
topMostViewController = presentedViewController
}
return topMostViewController
}
func sendEmail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["support#supportemail.com"])
mail.setSubject("In-App Feedback")
mail.setMessageBody("", isHTML: false)
self.getTopMostViewController()!.present(mail, animated: true, completion: nil)
} else {
print("Failed To Send Email!")
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
I have also tried setting the UINavigationControllerDelegate in the sendEmail() function.
mail.delegate = self as? UINavigationControllerDelegate
I have also tried things like popping the view controller and going back to the top most view controller in the mailComposeController.
popToRootViewContoller(animated: true)
getTopMostViewController()?.dismiss(animated: true, completion: nil)
I've tried following the guide on, https://developer.apple.com/documentation/messageui/mfmailcomposeviewcontroller, but it didn't work as I think my scenario is different since I am going from a SKScene to the MFMailCompose ViewController then back to a SKScene.
I'm one of the other developers working on this project. Posting in case someone has similar problems.
We were attempting to call our FeedbackVC in a way that looked like this:
if nodeTapped.name == "Feedback" {
let vc = FeedbackVC()
vc.emailButtonTapped(foo)
}
This would create the FeedbackVC class, call the emailButtonTapped method, and then deallocate the class from memory upon exiting the if statement. This means that clicking cancel or send would attempt to access the deallocated space, causing an EXC_BAD_ACCESS error. I fixed this by declaring vc as a class variable instead of declaring it inside the if statement.

MFMailComposeViewController delegate not working on swift 4

I'm trying to dismiss the MFMailComposeViewController but the delegate is not triggered. It seems that it's a common issue and the answers are the same, but they do not work for me :(. I have a button that calls the function to send the mail. I first create a csv file, then the MFMailComposeViewController and attach the csv file to the mail. The mail is sent sometimes (the mail view controller does not dismiss after that) and the cancel button shows me the option to delete or save the draft but after that nothing happens.
Here's the code of the button:
import UIKit
import MessageUI
class UserInfoViewController: UIViewController, MFMailComposeViewControllerDelegate {
#IBAction func uploadPressed(_ sender: Any) {
let contentsOfFile = "Name,ID,Age,Sex,Time,\n\(name),\(id),\(age),\(sex),\(time)"
let data = contentsOfFile.data(using: String.Encoding.utf8, allowLossyConversion: false)
if let content = data {
print("NSData: \(content)")
}
func configuredMailComposeViewController() -> MFMailComposeViewController {
let emailController = MFMailComposeViewController()
//emailController.mailComposeDelegate = self as? MFMailComposeViewControllerDelegate
emailController.mailComposeDelegate = self
emailController.setToRecipients([""])
emailController.setSubject("CSV File")
emailController.setMessageBody("", isHTML: false)
emailController.addAttachmentData(data!, mimeType: "text/csv", fileName: "registro.csv")
return emailController
}
let emailViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
self.present(emailViewController, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
print("Delegate worked!")
controller.dismiss(animated: true, completion: nil)
}
}
}
Thank you very much in advance.
Your issue is being caused by putting the delegate method inside another method. You can't do that. Delegate functions need to be at the top level of the class. Simply move your mailComposeController(_:didFinishWith:error:) function out of the uploadPressed function.

Page created by MFMailComposeViewController could not be shown because of hierarchy - Swift 4

I attempt to add a function, that is a mail page would pop up after the user touched a row in a table. Namely, it means that the user could activate a "function" (here the name of that function is "orderOfSendAnEmailToReportTheProblem") when the row is tapped. All of my codes were shown below. (This kind of code has been proposed by several genii on Stackoverflow...)
import Foundation
import UIKit
import MessageUI
class ReportProblem : UIViewController, MFMailComposeViewControllerDelegate {
func orderOfSendAnEmailToReportTheProblem() {
let mailComposeViewController = configureMailController()
if MFMailComposeViewController.canSendMail() {
self.present(mailComposeViewController, animated: true, completion: nil)
} else {
showMailError()
}
}
//Activate the series of the commands of sending the email.
func configureMailController() -> MFMailComposeViewController {
let mailComposeVC = MFMailComposeViewController()
mailComposeVC.mailComposeDelegate = self
mailComposeVC.setToRecipients(["my email"])
mailComposeVC.setSubject("Yo")
return mailComposeVC
}
//Set the recipient and the title of this email automatically.
func showMailError() {
let sendMailErrorAlert = UIAlertController(title: "Could not sned the email.", message: "Oops, something was wrong, please check your internet connection once again.", preferredStyle: .alert)
let dismiss = UIAlertAction(title: "Ok", style: .default, handler: nil)
sendMailErrorAlert.addAction(dismiss)
self.present(sendMailErrorAlert, animated: true, completion: nil) //If you conform the protocol of NSObject instead of UIViewController, you could not finish this line successfully.
}
//Set a alert window so that it would remind the user when the device could not send the email successfully.
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
//Set this final step so that the device would go to the previous window when you finish sending the email.
}
However, a problem occurred. When I test it on my real device, and after I tapped that particular row, nothing happened, no any new page pop up... The Xcode only showed that "Warning: Attempt to present on whose view is not in the window hierarchy!" I have tried several ways, such as "view.bringSubview(toFront: mailComposeVC)" or adding the codes shown below at the end of my codes, but nothing worked.
func topMostController() -> UIViewController {
var topController: UIViewController = UIApplication.shared.keyWindow!.rootViewController!
while (topController.presentedViewController != nil) {
topController = topController.presentedViewController!
}
return topController
}
I noticed that some other people also would face similar problems when they want to create the alert window, and the solution of that is to create an independent UIWindow, but I want to use mailComposeController to present the email page instead. Some others also faced some problems about MFMailComposeViewController, but their problems are not concerning to hierarchy. I was a novice of swift, and I was haunted by this problem for a whole day... I used swift 4 to develop my App, is anyone know how to solve this problem here?...
So now I'm writing another way to present which I'm using for generic views.
Have Some code in another class for presentation of view so that you can reuse them throughout the app with these two methods.
func slideInFromRight(parentView:UIView,childView:UIView) {
childView.transform = CGAffineTransform(translationX: parentView.frame.maxX, y: 0)
parentView.addSubview(childView)
UIView.animate(withDuration: 0.25, animations: {
childView.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
func slideOutToRight(view:UIView) {
UIView.animate(withDuration: 0.25, animations: {
view.transform = CGAffineTransform(translationX: view.frame.maxX, y: 0)
},completion:{(completed:Bool) in
view.removeFromSuperview()
})
}
Now use these methods to present and remove custom view controller as follows
let window = UIApplication.shared.keyWindow
let vc = YourViewController().instantiate()
self.addChildViewController(vc)
let view = vc.view
view.frame = CGRect(x: 0, y: 20, width: window!.frame.width, height: window!.frame.height-20)
//Here Animation is my custom presenter class and shared is it's shared instance.
Animation.shared.slideInFromRight(parentView: window!, childView: view)
//Or you can use current View controller's view
Animation.shared.slideInFromRight(parentView: self.view!, childView: view)
Genius Vivek Singh, your way looks good, but it's a little bit tedious. Moreover, it still did not work in my project... (It seems that you used some codes about UIView, such as parentView, childView, and view. However, I used MFMailComposeViewController which seems is a little bit different from original view...I am not sure whether this theory is correct or not...)
However, I have found the solution. I presume that the problem is that after the user clicked the row in another tableViewController (here is SettingTVController), it would activate the function "orderOfSendAnEmailToReportTheProblem( )" which is in "another " viewController (here is ReportProblem). Because there are two different viewController, some kind of conflict occurred.
Therefore, I move my whole codes I posted in the above question to my original tableViewController, so that the user would not go into another viewController when they activate the function, and there's no hierarchy problem anymore.
import UIKit
import StoreKit
import MessageUI
class SettingTVController: UITableViewController, MFMailComposeViewControllerDelegate {
var settingTitleConnection = showData()
override func viewDidLoad() {
//skip
}
override func didReceiveMemoryWarning() {
//skip
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//skip
}
override func numberOfSections(in tableView: UITableView) -> Int {
//skip
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//skip
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.indexPathForSelectedRow?.row == 2 && tableView.indexPathForSelectedRow?.section == 1 {
orderOfSendAnEmailToReportTheProblem()
} else {
//skip
}
tableView.deselectRow(at: indexPath, animated: true)
}
//-----<The codes below is used to construct the function of reporting problem with email>-----
func orderOfSendAnEmailToReportTheProblem() {
let mailComposeViewController = configureMailController()
self.present(mailComposeViewController, animated: true, completion: nil)
if MFMailComposeViewController.canSendMail() {
self.present(mailComposeViewController, animated: false, completion: nil)
} else {
showMailError()
}
}
//Activate the series of the commands of sending the email.
func configureMailController() -> MFMailComposeViewController {
let mailComposeVC = MFMailComposeViewController()
mailComposeVC.mailComposeDelegate = self
mailComposeVC.setToRecipients(["datototest#icloud.com"])
mailComposeVC.setSubject("Reporting of Problems of Rolling")
return mailComposeVC
}
//Set the recipient and the title of this email automatically.
func showMailError() {
let sendMailErrorAlert = UIAlertController(title: "Could not send the email.", message: "Oops, something was wrong, please check your internet connection once again.", preferredStyle: .alert)
let dismiss = UIAlertAction(title: "Ok", style: .default, handler: nil)
sendMailErrorAlert.addAction(dismiss)
self.present(sendMailErrorAlert, animated: true, completion: nil) //If you conform the protocol of NSObject instead of UIViewController, you could not finish this line successfully.
}
//Set a alert window so that it would remind the user when the device could not send the email successfully.
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
//UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)
}
//Set this final step so that the device would go to the previous window when you finish sending the email.
//-----<The codes above is used to construct the function of reporting problem with email>-----
}
I posted my codes above so that it may help others who face similar problem someday. Once again, thanks for your help!!
I don't know why you are facing a view hierarchy issue. But I am able to achieve the mail share option in swift 4. I followed exactly same steps.
Check if mail can be sent:
MFMailComposeViewController.canSendMail()
Configure mail body:
private func configureMailController() -> MFMailComposeViewController {
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.mailComposeDelegate = self
mailComposeViewController.setMessageBody("MESSAGE BODY", isHTML: true)
return mailComposeViewController
}
Present mail VC:
present(mailComposeViewController, animated: true)
confirm optional protocol and dismiss the view explicitly:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}

MessageComposeViewController not calling delegate

I've been searching for solutions to my problem without any success...
The app I'm developing lets the user play a small quiz game and send the result as a text message. Everything works fine except when the MessageComposeViewController is suppose to dismiss (on send/cancel).
It seems like the MessageComposeViewController doesn't call the delegate since I don't get the print from the delegate function...
I have a separate class called SendMessage which handles the MessageComposeViewController, when the user click a button "Send" in a ViewController I create an instance of this class and present it.
Part of my ViewController with the send button:
#IBAction func Send(_ sender: Any) {
let sendResult = SendMessage()
if sendResult.canSend() {
let meddelande = sendResult.createMessage(result: 8, name: "Steve Jobs")
present(meddelande, animated: true, completion: nil)
} else {
alert.addAction(alertButton)
self.present(alert, animated: true, completion: nil)
}
}
The class which handles the MessageComposeViewController called SendMessage (I left some irrelevant code out)
func createMessage(result: Int, name: String) -> MFMessageComposeViewController {
let meddelande = MFMessageComposeViewController()
meddelande.messageComposeDelegate = self
meddelande.recipients = ["PhoneNumber"]
meddelande.body = name + ": " + String(result)
return meddelande
}
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
print ("F*ck")
controller.dismiss(animated: true, completion: nil)
}
Grateful for any help!
I think you should hold a strong reference to it instead of a local variable
let sendResult = SendMessage()
declare it as instance variable
var sendResult:SendMessage?

how to wait for ui delegate in swift

I want to create simple object helper to sending sms in many places on my Swift 2.0 app, and in next step another helpers (email, pdf opener etc)
I create simple class:
import Foundation
import MessageUI
class SmsHelper: MFMessageComposeViewControllerDelegate {
func sendSMS(body: String){
if (MFMessageComposeViewController.canSendText()){
let messageVC = MFMessageComposeViewController()
messageVC.body = body
//messageVC.recipients = ["Enter tel-nr"]
messageVC.messageComposeDelegate = self;
AppDelegate().sharedInstance().getTopController().presentViewController(messageVC, animated: false, completion: nil)
}
else{
//do some alert etc.
}
}
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult){
sendSMSRsp(result, errorMsg: nil)
controller.dismissViewControllerAnimated(true, completion: nil)
print("sms didFinishWithResult")
}
}
in anywhere in code I want to do that:
class someAnotherClass{
func someFunction(){
let smsHelper = SmsHelper()
smsHelper.sendSms("some text")
}
}
so sms ios editor is opened, but when I want to close it or sending, it don't dismiss, function messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) i never called and app crashes with memory leak, I know reason: is because SmsHelper object in 'someFunction' is deleting after end scope of this function, and this object is nil, and system try to call didFinishWithResult at nil object. I confirm it: when I add smsHelper object as member of 'SomeClass' it worked - delegate is called.
Question is: What is the best practice to do that, adding a member is not an option for me, because many classes can use that, also creating a singleton, appDelegate member is I think quite stupid. How to force not deleting a object at end of scope function?
Try to use singleton way to do it:
class SmsHelper: MFMessageComposeViewControllerDelegate {
static let sharedInstance = SmsHelper()
private init() {}
}
And usage:
SmsHelper.sharedInstance().sendSms('some text')
EDIT:
If you add different init method from abstract class.
private override init(message: String){
super.init()
}
then change definition to static let sharedInstance = SmsHelper(message:"smsHelper") won't complicated.
For your delegate, just assign it out of the init method, in the UIViewController viewDidLoad,
SmsHelper.sharedInstance().delegate = self
For me, the way to do this is to create a type alis of a closure, then storing an optional variable of this type alias. When you call sendSMS, you pass in a closure and set it to your variable. Then that can be called in the delegate, which will then pass it through the closure of sendSMS:
import Foundation
import MessageUI
class SmsHelper: MFMessageComposeViewControllerDelegate {
let messageVC: MFMessageComposeViewController!
typealias SMSCompletion = (result: MessageComposeResult, errorMsg: NSError?) -> ()
var sendSMSRsp: SMSCompletion?
func sendSMS(body: String, completion: SMSCompletion){
sendSMSRsp = completion
if (MFMessageComposeViewController.canSendText()){
messageVC = MFMessageComposeViewController()
messageVC.body = body
//messageVC.recipients = ["Enter tel-nr"]
messageVC.messageComposeDelegate = self;
AppDelegate().sharedInstance().getTopController().presentViewController(messageVC, animated: false, completion: nil)
} else {
//do some alert etc.
}
}
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult){
sendSMSRsp?(result: result, errorMsg: nil)
controller.dismissViewControllerAnimated(true, completion: nil)
print("sms didFinishWithResult")
}
}
class SomeOtherClass {
func someFunction() {
let smsHelper = SmsHelper()
smsHelper.sendSMS("some text") { (result, errorMsg) -> () in
}
}
}

Resources