Implementing MailComposeController in tableView - ios

I have created the setting page in TableView. When I tap row 0 UIActivityViewController is called. When I tap row2 MailComposeController is called.
MailComposeController is called, but when I tap cancel or send button on mail screen it does not work.
Here is the code。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
let message = "Hey download my app [LINK]"
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(["address#gmail.com"])
mailCompose.setSubject("feedback")
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)
}

You have to put the delegate method in the ViewController but not in the tableView:didSelectRowAt method:
extension ViewController: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}

Implement the delegate methods of Mail composer on click of send and cancel button:
Here are the Delegates methods which you need to implement :
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: {
switch (result)
{
case .cancelled:
break
case .saved:
break;
case .sent:
AppUtility.showAlert(message: kEmailSentSuccessMessage, isSuccess: true)
break;
case .failed:
AppUtility.showAlert(message: kEmailSentFailureMessage, isSuccess: true)
break;
}
})
}
Use breakpoint to see what exactly going on the click of send or cancel. and make sure that you have created the MailComposer object locally . since this object is going to send and cancel the mail each time .

Related

Dismiss a ShortcutViewController after setting it

I Implements SiriShortcut with Intent in my App.
I set up a button "Add to Siri" with the code that Apple give here : https://developer.apple.com/documentation/sirikit/inuiaddvoiceshortcutviewcontroller
func addSiriButton(to view: UIView) {
let button = INUIAddVoiceShortcutButton(style: .blackOutline)
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
view.centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true
view.centerYAnchor.constraint(equalTo: button.centerYAnchor).isActive = true
button.addTarget(self, action: #selector(addToSiri(_:)), for: .touchUpInside)
}
// Present the Add Shortcut view controller after the
// user taps the "Add to Siri" button.
#objc
func addToSiri(_ sender: Any) {
if let shortcut = INShortcut(intent: ProjectorOnIntent()) {
let viewController = INUIAddVoiceShortcutViewController(shortcut: shortcut)
viewController.modalPresentationStyle = .formSheet
viewController.delegate = self as? INUIAddVoiceShortcutViewControllerDelegate // Object conforming to `INUIAddVoiceShortcutViewControllerDelegate`.
present(viewController, animated: true, completion: nil)
}
}
But once I record a phrase when I click on the "Ok" button the view won't dismiss.
The error I got in the console :
2018-10-12 10:16:51.985156+0200 AppName[1029:172350] [default] No results found for query: {(
<_LSApplicationIsInstalledQuery: 0x28226e560>
)}
2018-10-12 10:16:51.989467+0200 AppName[1029:172263] [strings] ERROR: Add to Siri not found in
table Localizable of bundle CFBundle 0x111a01d00 </var/containers/Bundle/Application/DDADF244-FBCE-47C0-90F8-E8C8ADA6962E/AppName.app> (executable, loaded)
hope it help you,
class ViewController: UIViewController, INUIAddVoiceShortcutViewControllerDelegate, INUIEditVoiceShortcutViewControllerDelegate {
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didUpdate voiceShortcut: INVoiceShortcut?, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didDeleteVoiceShortcutWithIdentifier deletedVoiceShortcutIdentifier: UUID) {
controller.dismiss(animated: true, completion: nil)
}
func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) {
controller.dismiss(animated: true, completion: nil)
}
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
Use INUIAddVoiceShortcutViewControllerDelegate and INUIEditVoiceShortcutViewControllerDelegate
Thank you.

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)
}

Can't dismiss MFMailComposeViewController in Swift 3.0

The MFMailComposeViewController cannot be dismissed after pressing cancel or send button. I have added MFMailComposeViewControllerDelegate in my class but still, it's not working?
Here is my code:
func sendEmail() {
let MailVC = MFMailComposeViewController()
MailVC.mailComposeDelegate = self
MailVC.setToRecipients(["\(emailLabel?.text)"])
MailVC.setSubject("Set your subject here!")
MailVC.setMessageBody("Hello this is my message body!", isHTML: false)
// Present the view controller modally.
self.present(MailVC, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
Delegate method signature is wrong. You are missing _ before controller parameter. Try this.
public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
And make sure this.
class ViewController: UIViewController ,MFMailComposeViewControllerDelegate

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