-->

MFMailComposeViewController Not Dismissing From Vi

2020-07-21 01:21发布

问题:

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:@"contact@drchrono.com"]; 
    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; 
    //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

    [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];
}

回答1:

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)



回答2:

Use the following code

- (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 the next time the user connects to email");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was nog saved or queued, possibly due to an error");
            break;
        default:
            NSLog(@"Mail not sent");
            break;
    }

    [self dismissModalViewControllerAnimated:YES];
}