Vignesh Babu
Vignesh Babu

Reputation: 690

Display the success alert message when mail send from MFMailComposeViewController in iPhone

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?

Upvotes: 3

Views: 8674

Answers (5)

Sudhi 9135
Sudhi 9135

Reputation: 765

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.")
        }

Upvotes: 1

nh32rg
nh32rg

Reputation: 1482

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.

Upvotes: 2

Anil Kothari
Anil Kothari

Reputation: 7733

(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;

Upvotes: 1

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

you should implement this method for the delegate object...

– mailComposeController:didFinishWithResult:error:

look at this for more detail... http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewControllerDelegate_protocol/Reference/Reference.html#//apple_ref/occ/intf/MFMailComposeViewControllerDelegate

Upvotes: 1

Rahul Juyal
Rahul Juyal

Reputation: 2144

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

Upvotes: 11

Related Questions