Search code examples
iosobjective-cmfmailcomposeviewcontroller

Mail Compose not dismiss when click cancel button


I have this code who send a e-mail to users, Im using MFMailCompose:

.h file

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface TestViewController : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>

@property(nonatomic,assign) id<MFMailComposeViewControllerDelegate> mailComposeDelegate;

.m file

@synthesize mailComposeDelegate

-(void)sendEmail:(NSString*)valor{

//Valor receive the email

    NSString *corpoMensagem = @"No have body yet...";

    // From within your active view controller
    if([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
        mailCont.mailComposeDelegate = self;// Required to invoke mailComposeController when send

        [mailCont setSubject:@"FazerBem - Recibo de Compra"];
        [mailCont setToRecipients:[NSArray arrayWithObject:valor]];
        [mailCont setMessageBody:corpoMensagem isHTML:YES];

        [self presentViewController:mailCont animated:YES completion:nil];
    }

}

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

    if (error){
        NSString *errorTitle = @"Erro to send";
        NSString *errorDescription = [error localizedDescription];
        UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:errorTitle message:errorDescription delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [errorView show];
    }

    [self becomeFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{


}

when I click the send button he can send the email to the recipient and can close the screen, now when I click the cancel button, it opens 2 more options 'delete draft' and 'save draft' when I click on one of the app crashes and returns me the following error:

[TestViewController respondsToSelector:]: message sent to deallocated instance 0x16b3b470

How I can solve this problem?


Solution

  • Use Switch case to for performing actions:

    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
    {
        UIAlertView *alert;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            break;
        case MFMailComposeResultSaved:
            alert = [[UIAlertView alloc] initWithTitle:@"Draft Saved" message:@"Composed Mail is saved in draft." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            break;
        case MFMailComposeResultSent:
            alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You have successfully referred your friends." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            break;
        case MFMailComposeResultFailed:
            alert = [[UIAlertView alloc] initWithTitle:@"Failed" message:@"Sorry! Failed to send." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            break;
        default:
            break;
    }
    
    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:nil];
    }