Search code examples
iosobjective-cmfmailcomposeviewcontroller

making MFMailComposeViewController disappear upon sending


So I know that i would have to use the MFMailComposeResultSent. (At least I think that's what i'm supposed to use) This is the code that i have and it sends the email and everything fine but the mail composer stays up.

EDIT: Here is my code

    if ([condition  isEqual: @"Excellent"] && [MFMailComposeViewController canSendMail]) {
    NSString *emailBody = [NSString stringWithFormat:@"Product:%@    Make:%@   Year Manufactured:%@  Description:%@  Condition:Excellent Email:%@",inputProduct,inputMake,inputYear,inputDescript, inputEmail];
    NSArray *recipient = [NSArray arrayWithObject:@"[email protected]"];
    MFMailComposeViewController *SuperLovedEmail = [[MFMailComposeViewController alloc]init];
    [SuperLovedEmail setTitle:emailTitle];
    [SuperLovedEmail setToRecipients:recipient];
    [SuperLovedEmail setMessageBody:emailBody isHTML:NO];
    [SuperLovedEmail setUserActivity:false];
    [self presentViewController:SuperLovedEmail animated:YES completion:nil];

}
else {
    UIAlertController *emailAlert = [UIAlertController alertControllerWithTitle:@"Oh No!" message:@"Your Phone is not able to send an email currently or you have not chosen a condition. Please make sure you have chosen a condition and that you are signed in through Mail" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *emailAlertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * emailAlertAction) {}];
    [emailAlert addAction:emailAlertAction];
    [self presentViewController:emailAlert animated:YES completion:nil];
}

Solution

  • You need to set the mailComposeDelegate property of your SuperLovedEmail object to be self, then handle the didFinishWithResult message, like this:

    - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
        [controller dismissViewControllerAnimated:true completion:nil];
    }
    

    You might find it useful to evaluate the MFMailComposeResult to see whether sending actually happened.