Reputation: 253
I am trying to implement MFMailComposeViewController() in two cells (Subject "feedback"and "feedback2").
When I select each cell, "feedback" works well, but "feedback2" is not called.
It seems not difficult issue but I find it difficult to fix the problem.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.row == 0 {
let message = "hey download this app"
let shareView = UIActivityViewController(activityItems: [message], applicationActivities: nil)
self.present(shareView, animated: true, completion: nil)
} else if indexPath.row == 1 {
let mailCompose = MFMailComposeViewController()
mailCompose.mailComposeDelegate = self
mailCompose.setToRecipients(["gmail.com"])
mailCompose.setSubject("feedback")
mailCompose.setMessageBody("text", isHTML: false)
if MFMailComposeViewController.canSendMail()
{
self.present(mailCompose, animated: true, completion: nil)
} else if indexPath.row == 2 {
let mailCompose = MFMailComposeViewController()
mailCompose.mailComposeDelegate = self
mailCompose.setToRecipients(["[email protected]"])
mailCompose.setSubject("feedback2")
mailCompose.setMessageBody("text", isHTML: false)
if MFMailComposeViewController.canSendMail()
{
self.present(mailCompose, animated: true, completion: nil)
}
else{
print("error...!")
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
Upvotes: 0
Views: 199
Reputation: 60150
Your else if indexPath.row == 2
is indented one level too deeply – its else
refers to !MFMailComposeViewController.canSendMail()
, not the previous indexPath
check. You'll need to shift that branch outwards one level to get the effect you want.
For the future, it might be easier to debug if you refactor out a single method for all your mail-composing:
private func sendMail(to recipient: String, subject: String) {
if !MFMailComposeViewController.canSendMail() {
return
}
let mailCompose = MFMailComposeViewController()
mailCompose.mailComposeDelegate = self
mailCompose.setToRecipients([recipient])
mailCompose.setSubject(subject)
mailCompose.setMessageBody("text", isHTML: false)
self.present(mailCompose, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0: /* activity view controller stuff */ break;
case 1:
sendMail(to: "gmail.com", subject: "feedback")
break;
case 2:
sendMail(to: "[email protected]", subject: "feedback2")
break;
}
}
Upvotes: 2