3
votes

I need to display the alert message when the user send successfully mail from the iPhone using MFMailComposeViewController.

I tried with didFinishWithResult delegate but it is calling for both send and cancel then how we can determine we successfully send the message?

5

5 Answers

11
votes
Try this code

-(IBAction)Btn_EmailPressed:(id)sender{
    if (![MFMailComposeViewController canSendMail]) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Email cannot be configure." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }else {
        picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate=self;
        [picker setToRecipients:nil];
                [picker setSubject:@"Email"];
                [picker setMessageBody:nil isHTML:NO];
                NSArray *toRecipients = [[NSArray alloc] initWithObjects:lblSite.text,nil];
                [picker setToRecipients:toRecipients];
                [self presentModalViewController:picker animated:YES];
            }
}


- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    NSString *msg1;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            msg1 =@"Sending Mail is cancelled";
            break;
        case MFMailComposeResultSaved:
            msg1=@"Sending Mail is Saved";
            break;
        case MFMailComposeResultSent:
            msg1 =@"Your Mail has been sent successfully";
            break;
        case MFMailComposeResultFailed:
            msg1 =@"Message sending failed";
            break;
        default:
            msg1 =@"Your Mail is not Sent";
            break;
    }
    UIAlertView *mailResuletAlert = [[UIAlertView alloc]initWithFrame:CGRectMake(10, 170, 300, 120)];
    mailResuletAlert.message=msg1;
    mailResuletAlert.title=@"Message";
    [mailResuletAlert addButtonWithTitle:@"OK"];
    [mailResuletAlert show];
    [mailResuletAlert release];
    [self dismissModalViewControllerAnimated:YES];  
}
2
votes

I had trouble with this approach. In my app I used MFMailComposeViewController for email and MFMessageComposeViewController for SMS messages and both didFinishWithResult routines used a similar approach to the one above, where an alert is shown before the VC is dismissed.

It seemed that if you sent an SMS, the next time you tried an email the cursor would not appear in the email body and you could not select any text. Also in the debugger I was getting "wait_fences: failed to receive reply: 10004003".

I eventually just removed the alert views from this part of the app and the problem went away. If anyone has a resolution for this issue I'd be glad to hear it.

1
votes
(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

Use this delegate and inside this MFMailComposeResult is an enum

enum MFMailComposeResult {
   MFMailComposeResultCancelled,
   MFMailComposeResultSaved,
   MFMailComposeResultSent,
   MFMailComposeResultFailed
};
typedef enum MFMailComposeResult MFMailComposeResult;
1
votes

Swift Version of the Answer.

       func sendMail(){
                if MFMailComposeViewController.canSendMail() {
                    let mail = MFMailComposeViewController()
                    mail.mailComposeDelegate = self
                    mail.setToRecipients(["[email protected]"])
                    present(mail, animated: true)
                } else {
                    showSendMailErrorAlert()
                }
            }

        func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            switch result {
                case .cancelled: print("Sending Mail is cancelled")
                case .sent : print("Your Mail has been sent successfully")
                case .saved : print("Sending Mail is Saved")
                case .failed : print("Message sending failed")
            }
            controller.dismiss(animated: true, completion: nil)
        }

        func showSendMailErrorAlert() {
            showAlert(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.")
        }