Displays a successful alert message when sending mail with MFMailComposeViewController on iPhone

I need to display a warning message when a user sends successful mail from iPhone using MFMailComposeViewController.

I tried with the delegate didFinishWithResult, but it causes both sending and cancellation, then how can we determine if we successfully sent a message?

+4
source share
5 answers
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]; } 
+12
source

I had problems with this approach. In my application, I used the MFMailComposeViewController for email and the MFMessageComposeViewController for SMS messages, and the didFinishWithResult routines used a similar approach to the above, where a warning is issued before the VC is rejected.

It seemed that if you sent SMS the next time you try the email, the cursor will not appear in the body of the message and you will not be able to select any text. Also in the debugger, I received "wait_fences: did not receive a response: 10004003".

In the end, I just removed the warnings from this part of the application, and the problem disappeared. If anyone has a solution to this question, I would be happy to hear it.

+2
source

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

 mailComposeController:didFinishWithResult:error: 

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

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

Use this delegate and inside this MFMailComposeResult is an enumeration

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

A quick version of the answer.

  func sendMail(){ if MFMailComposeViewController.canSendMail() { let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setToRecipients([" hello@gmail.com "]) 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.") } 
0
source

Source: https://habr.com/ru/post/1384688/


All Articles