MFMailComposeViewController not dismiss with Cancel button

563 views Asked by At

I've a class that subclass NSObject with a function do display a MFMailComposeViewController. Here is the code :

MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];

mailController.mailComposeDelegate = self;
[mailController setSubject:@"Sample Subject"];
[mailController setMessageBody:@"Here is some main text in the email!" isHTML:NO];
[mailController setToRecipients:@[self.email]];

UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
UINavigationController *navigationController = tabbarController.selectedViewController;
[navigationController.topViewController presentViewController:mailController animated:YES completion:NULL];

Everything works well with this code. The problem is when I want to dismiss the MFMailComposeViewController. Sometime I get a crash, sometimes it just don't happens nothing. I've implemented the delegate function :

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    UINavigationController *navigationController = tabbarController.selectedViewController;
    [navigationController.topViewController dismissViewControllerAnimated:YES completion:nil];
}

After that I tried to show and dismiss it directlty from a ViewController and everything was working. Even the cancel button.

I don't know why it works in my ViewController Class but not in my subclass of NSObject.

When I get the crash I've seen in the logs :

-[MFMailComposeInternalViewController _notifyCompositionDidFinish]
2

There are 2 answers

2
Naresh Reddy M On

Try this,

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{
    // Do your work and dismiss after you are done with it.
    [controller dismissViewControllerAnimated:YES completion:nil];
}

Hope that helps.

2
Dhiru On

Try this in your singleton class

    UIViewController *currentViewController;

    - (void)sendEmail:(id) viewController {

 currentViewController=(UIViewController*)viewController;

        NSString * appVersionString =@"";
        NSString *strEmailMessage=@"";
        NSString *strEmailSubject=@"";


        NSArray *toRecipents =@"";



        if ([MFMailComposeViewController canSendMail]) {

            MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
            mc.mailComposeDelegate =viewController;
            [mc setSubject:strEmailSubject];
            [mc setMessageBody:strEmailMessage isHTML:NO];
            [mc setToRecipients:toRecipents];


            [viewController presentViewController:mc animated:YES completion:NULL];


        }
        else{
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" message:@"Please setup email account" delegate:nil cancelButtonTitle:@"cancle" otherButtonTitles:nil];

            [alert show];
        }

    }

set delegate like this

 mc.mailComposeDelegate =viewController;

to dissmiss viewController

 - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        switch (result)
        {
            case MFMailComposeResultCancelled:

                break;
            case MFMailComposeResultSaved:
               break;
            case MFMailComposeResultSent:

                break;
            case MFMailComposeResultFailed:

                break;
        }

        // Close the Mail Interface
        [currentViewController dismissViewControllerAnimated:YES completion:NULL];
    }

i hope this will work...