Crashing in MFMailComposeViewController implementation

282 views Asked by At

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:@"[email protected]", 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

1

There are 1 answers

7
Mrug On BEST ANSWER

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];


}