Search code examples
iphoneobjective-ccocoa-touchmfmailcomposeviewcontroller

MFMailComposeViewController Not Dismissing From View


I have the following code that is called when a button on an action sheet is called. But when I press cancel, then delete draft, it just feezes and does not dismiss. I use the same code elsewhere in my app and its called from a select of a tableview cell and it works find there. Any ideas why it isn't working here?

Ther is also no error msg in the console when it freezes.

if([MFMailComposeViewController canSendMail])
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"Dr. Chrono Support"];

    NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString* versionNum = [infoDict objectForKey:@"CFBundleVersion"];
    NSString *appName = [infoDict objectForKey:@"CFBundleDisplayName"];
    NSString *text = [NSString stringWithFormat:@"%@ %@",appName,versionNum];

    // Set up recipients
    NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 
    //NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 

    [picker setToRecipients:toRecipients];
    //[picker setCcRecipients:ccRecipients];  
    //[picker setBccRecipients:bccRecipients];

    // Attach an image to the email
    //NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
    //NSData *myData = [NSData dataWithContentsOfFile:path];
    //[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];

    // Fill out the email body text
    NSString *emailBody = text;
    [picker setMessageBody:emailBody isHTML:NO];

    [self presentModalViewController:picker animated:YES];
    [picker release];
}

Solution

  • You need to implement the delegate method:

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

    Then in this delegate method add:

    [self dismissModalViewControllerAnimated:YES];
    

    and it should be working just fine.

    You don't have to look for the result (only if you want to display a "Thank you" alert or something, for example, if the user did indeed hit Send)