1

My app crashes when I try to perform sent or cancel button actions in MFMailComposeViewController.

here is my code. I have imported message framework and added delegates in .h file.

     NSString *emailTitle = @"Test Email";

                NSString *messageBody = @"Welcome Guest";

                NSArray *recipentsArray = [[NSArray alloc]initWithObjects:@"xxx@gmail.com", nil];
            [[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

             [[UINavigationBar appearance] setBackgroundColor:nil];
                MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];

                mc.mailComposeDelegate = self;

                [mc setSubject:emailTitle];

                [mc setMessageBody:messageBody isHTML:NO];

                 [mc setToRecipients:recipentsArray];

                [self presentViewController:mc animated:YES completion:NULL];(void) mailComposeController:(MFMailComposeViewController *)controller 
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        switch (result)
        {
            case MFMailComposeResultCancelled:
                NSLog(@"Mail cancelled");
                break;
            case MFMailComposeResultSaved:
                NSLog(@"Mail saved");
                break;
            case MFMailComposeResultSent:
                NSLog(@"Mail sent");
                break;
            case MFMailComposeResultFailed:
                NSLog(@"Mail sent failure: %@", [error localizedDescription]);
                break;
            default:
                break;
        }

        // Close the Mail Interface
        [self dismissViewControllerAnimated:YES completion:NULL];
    }

This is how my mf mail composer looks like :----

enter image description here

Mrug
  • 4,963
  • 2
  • 31
  • 53
ios
  • 955
  • 1
  • 12
  • 35

1 Answers1

0

Just make the MFMailComposeViewController *mc in global. I mean declare it outside this method. It's crashing because at the end of method Your mc gets deallocated.

Interface

@interface Demo()
{
   MFMailComposeViewController *mc;
}
@end

Implementation

-(void)ShareViaEmail {

objMailComposeController = [[MFMailComposeViewController alloc] init];
objMailComposeController.mailComposeDelegate = self;

if ([MFMailComposeViewController  canSendMail]) {

    [objMailComposeController setSubject:@"Hello"];
    [objMailComposeController setMessageBody:@"This is Body of Message" isHTML:NO];

        NSData *ImageData = [NSData dataWithContentsOfFile:self.aStrSourceName];
        NSString *mimeType;

        if ([[self CheckExtensionOfURL:self.aStrSourceName]isEqualToString:@"Image"]) {
            mimeType = @"image/png";
        }
        else if ([[self CheckExtensionOfURL:self.aStrSourceName]isEqualToString:@"PDF"]) {
            mimeType = @"application/pdf";
        } else if ([[self CheckExtensionOfURL:self.aStrSourceName]isEqualToString:@"Audio"]) {
            mimeType = @"audio/mpeg";
        } else if ([[self CheckExtensionOfURL:self.aStrSourceName]isEqualToString:@"Video"]) {
            mimeType = @"video/mp4";
        }


        [objMailComposeController addAttachmentData:ImageData mimeType:mimeType fileName:@"attachement"];

    }
    [self.navigationController presentViewController:objMailComposeController animated:YES completion:Nil];

}

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

switch (result) {
    case MFMailComposeResultCancelled:
        [self ShowAlertView:kMsgMailCancelled];
        break;

    case MFMailComposeResultFailed:
         [self ShowAlertView:kMsgMailFailed];
        break;

    case MFMailComposeResultSaved:
         [self ShowAlertView:kMsgMailSaved];
        break;

    case MFMailComposeResultSent:
         [self ShowAlertView:kMsgSent];
        break;
    default:
        break;
}

// CHECK OUT THIS, THIS MIGHT BE IN YOUR CASE
[self.navigationController dismissViewControllerAnimated:controller completion:nil];


}
Mrug
  • 4,963
  • 2
  • 31
  • 53
  • I tried your solution , but still app crashed on button click. – ios Jun 18 '15 at 06:59
  • I have updated the code.. Please checkout the way of Dismissing controller in your case. – Mrug Jun 18 '15 at 07:11
  • App still crashes . I don`t know whats wrong with my code. – ios Jun 18 '15 at 07:18
  • Can you share your actual code only of MailComposer ? – Mrug Jun 18 '15 at 07:21
  • [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:109.0/255.0 green:135.0/255.0 blue:200/255.0 alpha:1.0]]; i used this to color my apps navigation bar , can this be a problem ? – ios Jun 18 '15 at 07:24
  • Just tried commenting these lines , still the crash resides – ios Jun 18 '15 at 07:33
  • I want to open this mailComposer on a uialertview buttons click @Mrug – ios Jun 18 '15 at 08:01