objective-cmfmailcomposer

Dismiss mailComposeController


I am trying to dismiss the mail from my app after it's done if user sends or cancels. But for some reasone this never dismisses. I tried almost everything. I have also logged this so I will see if it went to dissmiss method. And the problem is there since it never enters the dismiss method.

What am i doing wrong???

- (IBAction)sendmail:(id)sender{
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData * imageData = UIImageJPEGRepresentation(image, 1.0);

    if ( [MFMailComposeViewController canSendMail] ) {
        MFMailComposeViewController * mailComposer = [[MFMailComposeViewController alloc] init];
        mailComposer.delegate = self;
        [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"];

        [mailComposer setSubject:@"Hello from My App!"];

        NSString *emailBody = @"Sent from My App, Still not in AppStore!";
        [mailComposer setMessageBody:emailBody isHTML:YES];

        [self presentModalViewController:mailComposer animated:YES];
    }
}

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{ 
    [self dismissModalViewControllerAnimated:YES];

    NSLog (@"mail finished"); // NEVER REACHES THIS POINT.
}

Solution

  • you could replace this line:

    [self dismissModalViewControllerAnimated:YES];
    

    with the following line:

    [controller dismissModalViewControllerAnimated:YES];