Search code examples
iphoneiosxcodemfmailcomposeviewcontrollerdismiss

MFMailComposeViewController : gives EXC-BAD-EXCESS when dismiss it from parent view


I have UINavigationController/UITabBarController base application and all is working fine but MFMailComposeViewController driving me crazy. I have go through almost every post of stack overflow about MFMailComposeViewController and tried it but than after it's give me EXC-BAD-EXCESS when i am trying to dismiss it from parent view.

Here is my code for MFMailComposeViewController.

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

UIAlertView *alert;
if (result == MFMailComposeResultFailed) {
    alert = [[UIAlertView alloc] initWithTitle:@"Message Failed!" message:@"Your email has failed to send" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];

    [alert show];
    [alert release];
}

[self dismissModalViewControllerAnimated:YES];
//[controller dismissModalViewControllerAnimated:YES];
//[[self parentViewController]dismissModalViewControllerAnimated:YES];
}

I don't know where is my mistake or what is wrong with this mail composer. Pleas provide some guide line.

For more code..here i ma providing code...

-(void)mailsender
 {
  NSUserDefaults *invoiceautogenerator = [NSUserDefaults standardUserDefaults];
 NSString *invoicestring = [invoiceautogenerator objectForKey:@"invoice"];

NSString *MemoString = [[NSUserDefaults standardUserDefaults]valueForKey:@"SALEMEMODETAIL"];
NSString *PassUniqeNumer = [[NSUserDefaults standardUserDefaults]valueForKey:@"UNIQUREFKEYDATA"];

MFMailComposeViewController *mail1 = [[[MFMailComposeViewController alloc] init]autorelease];

mail1.mailComposeDelegate = self;


[self databaseOpen];
NSString *str = [NSString stringWithFormat:@"Select EmailID from tblMercuryDetail"];
NSLog(@"str:%@",str);
NSArray *Query = [[NSArray alloc]init];
Query = [database executeQuery:str];
NSLog(@"Array DataL%@",Query);

NSString *Emailid = [NSString stringWithFormat:@"%@",[Query valueForKey:@"EmailID"]] ;
NSLog(@"Emailid:%@",Emailid);

Emailid = [Emailid stringByReplacingOccurrencesOfString:@"(" withString:@""];
Emailid = [Emailid stringByReplacingOccurrencesOfString:@")" withString:@""];
Emailid = [Emailid stringByReplacingOccurrencesOfString:@"\"" withString:@""];
Emailid = [Emailid stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"Emailid:%@",Emailid);
[Query release];
[database close];



UIImage *savedimage = [self loadImage:invoicestring];

NSData *myData = UIImagePNGRepresentation(savedimage);

[mail1 addAttachmentData:myData mimeType:@"image/png" fileName:@"sign.png"];

if ([MFMailComposeViewController canSendMail]) {
    //Setting up the Subject, recipients, and message body.
    [mail1 setToRecipients:[NSArray arrayWithObjects:Emailid,nil]];
    [mail1 setSubject:@"Receipt"];
    [mail1 setMessageBody:@"Message of email" isHTML:NO];
    NSString *emailBody;

    [self databaseOpen];

    NSString *str = [NSString stringWithFormat:@"Select * from tblGiftTransaction where InvoiceNo = '%@' ",invoicestring];

    NSArray *Arraydata = [[NSArray alloc]init];
    Arraydata = [database executeQuery:str];

    NSString *PurchaseAmt = [NSString stringWithFormat:@"%@",[Arraydata valueForKey:@"PurchaseAmt"]];
    NSString *TransactionType = [NSString stringWithFormat:@"%@",[Arraydata valueForKey:@"TransactionType"]];

    NSLog(@"==%@",Arraydata);


              emailBody = [NSString stringWithFormat:@
                             "<br>Memo:                  </br> " "%@"
                             "<br>Ref Number:            </br> ""%@"
                             "<br>Invoice No:            </br> ""%@"
                             "<br>Purchase Amount:       </br> ""$%@"
                             "<br>Transaction Type:      </br> ""%@"
                            ,MemoString,PassUniqeNumer,invoicestring,PurchaseAmt,TransactionType
                           ]; 

    NSLog(@"%@",emailBody);

            [mail1 setMessageBody:emailBody isHTML:YES];
    [[self parentViewController] presentModalViewController:mail1 animated:YES];
    [database close];


        }  

  //[mail release];
 }

Thanks.


Solution

  • I recently did this for my project, and it's working fine. I'm not %100 sure what's wrong with your code, however here is my code, if you wish to compare. Please comment if you need more help.

    - (IBAction)EmailMe:(id)sender
    {
        //Open MFMail and set Preferences
    
        UIGraphicsBeginImageContext(self.view.frame.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
        [mailComposer setMailComposeDelegate:self];
        NSData *imageData = UIImageJPEGRepresentation(image,1.0);
        if([MFMailComposeViewController canSendMail]){
            [mailComposer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
            [mailComposer setSubject: @"Subject"];
            [mailComposer setMessageBody:@"Hello, \n\n how are you?" isHTML:NO];
    //if you want to attach an image
    
    
    [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"];
            [mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    
    
    
            [self presentModalViewController:mailComposer animated:YES];
        }
    
    }
    
    //Dismiss MFmail
    -(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        if(error)
        {
            UIAlertView *alert= [[UIAlertView alloc]
                                 initWithTitle:@"Error %@"
                                 message:[NSString stringWithFormat:@"Error %@", [error description]]
                                 delegate:self  
                                 cancelButtonTitle:@"Dismiss" 
                                 otherButtonTitles:nil];
            [alert show];
    
        }
    
        [self dismissModalViewControllerAnimated:YES];
    }