Can't dismiss email composer view on iPhone?

I am new to iphone development. I created a tab based application. First, I want the email composer to be displayed. I can display it, but the cancel and send button does not work, I do not know where I am mistaken. Please help me. Here is my code.

- (void)viewDidLoad { [super viewDidLoad]; [self displayComposerSheet]; } -(void)displayComposerSheet { picker = [[MFMailComposeViewController alloc] init]; [[picker navigationBar] setTintColor:[UIColor blackColor]]; picker.mailComposeDelegate = self; if ([MFMailComposeViewController canSendMail]) { [picker setToRecipients:[NSArray arrayWithObjects:@" name@gmail.com ",nil]]; [picker setSubject:@"Sample"]; } [self.view addSubview:picker.view]; [self presentModalViewController:picker animated:YES]; } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [self dismissModalViewControllerAnimated:YES]; } 
+5
source share
4 answers

If you add only subview for mailcomposser, you need to remove it from self.view.In your code, you add subview and are also present,

If you use, use [self.view addSubview:picker.view]; than try to remove it.

  - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [controller.view removeFromSuperview]; } 

I suggest using

[self.navigationController presentModalViewController:picker animated:YES]; for the current MFMailComposeViewController,

and use [self dismissModalViewControllerAnimated:YES]; to reject it.

+3
source

You represent the postal composer twice.

Delete the line:

 [self.view addSubview:picker.view]; 

And replace the following line as follows:

 [self.navigationController presentModalViewController:picker animated:YES]; 
+1
source

Set delegate MFMailComposeViewController

 MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc]init]; mailcomposer.mailComposeDelegate = self; 

And use this delegation method

 -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { } 
0
source

Use this code:

 MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; controller.mailComposeDelegate = self; NSArray *toRecipients = [NSArray arrayWithObjects:@" niftyapplications@gmail.com ", @" support@niftysol.com ", nil]; [controller setToRecipients:toRecipients]; [controller setTitle:@"Contact Us"]; controller.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:controller animated:YES]; - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [self becomeFirstResponder]; NSString *strMailResult; switch (result) { case MFMailComposeResultCancelled: strMailResult = NSLocalizedString(@"E-Mail Cancelled",@""); break; case MFMailComposeResultSaved: strMailResult = NSLocalizedString(@"E-Mail Saved",@""); break; case MFMailComposeResultSent: strMailResult = NSLocalizedString(@"E-Mail Sent",@""); break; case MFMailComposeResultFailed: strMailResult = NSLocalizedString(@"E-Mail Failed",@""); break; default: strMailResult = NSLocalizedString(@"E-Mail Not Sent",@""); break; } UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ISO Audit",@"") message:strMailResult delegate:self cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil]; [alertView show]; [self dismissModalViewControllerAnimated:YES]; } 
0
source

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


All Articles