0

The MFMailComposeViewController is dismissing immediately when appear

- (IBAction)btnContactPressed:(id)sender
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        [mailer setSubject:@"Feedback"];

        NSArray *toRecipients = [NSArray arrayWithObjects:@"salimullah240@gmail.com", nil];
        [mailer setToRecipients:toRecipients];

        [self presentViewController:mailer animated:YES completion:nil];
    } 
    else    
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                        message:@"Your device doesn't support the composer sheet"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }

    // Remove the mail view
    [self dismissViewControllerAnimated:YES completion:nil];
}
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
SoftCoder
  • 167
  • 1
  • 2
  • 10

1 Answers1

0

Your code is correct. I tried and worked fine. The MFMailComposeViewController component can't be tested in the iOS simulator only in a device.

If you look this Thread in Apple Developer Forums the problem has a ticket in Apple Bug Report but still without any fix.

Also, just make sure you are importing:

#import <MessageUI/MFMailComposeViewController.h> 

and adding the delegate:

@interface ViewController () <MFMailComposeViewControllerDelegate>
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
  • A extra tip to your code is: `UIAlertView` is deprecated. Use `UIAlertController` [see more](http://stackoverflow.com/a/32690371/4850561). – Haroldo Gondim Apr 12 '16 at 12:51