Can't dismiss the email composer view in iPhone?
Asked Answered
P

4

4

I am new to iphone development.I have created a tabbar based application . In the first i want the email composer to be displayed. I am able to display it but the cancel and send button are not working,I don't know where do i go wrong .Please help me out. 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:@"[email protected]",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];

 }
Pennipennie answered 27/3, 2010 at 17:51 Comment(0)
C
1

You are presenting the mail composer twice.

Remove the line:

[self.view addSubview:picker.view];

And replace the next line with:

[self.navigationController presentModalViewController:picker animated:YES];
Corncob answered 27/3, 2010 at 21:5 Comment(2)
Sorry ,now i am not able to see the mail composer view itself.Pennipennie
I want the tab-bar visible in the mail composer viewPennipennie
M
1

If you are adding only subview of mailcomposser you have to remove it from self.view, In your code you are adding subview and present also,

If you are use only use [self.view addSubview:picker.view]; than Try with to remove it.

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

 }

I'm still suggest to use

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

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

Melburn answered 11/12, 2013 at 6:2 Comment(0)
I
0

Set Delegate of MFMailComposeViewController

MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc]init];

mailcomposer.mailComposeDelegate = self; 

And Use this Delegate Method

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
}
Impoverished answered 11/12, 2013 at 5:39 Comment(0)
R
0

Use this code:

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", 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];
}
River answered 11/12, 2013 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.