Vyacheslav
Vyacheslav

Reputation: 27211

MFMailComposeViewController doesn't close

I have an app where I use E-Mail to developer Button. When I press the button VEMailView-controller opens. This is just wrapper for MFMailComposeViewController.

When I press "send" button, the controller have to be closed but I see just white window. No more. It must be closed to main ViewController. How to fix?

This is my code:

    #import <MessageUI/MessageUI.h>

#import "VEMailView.h"

@interface VEMailView () <
    MFMailComposeViewControllerDelegate,
    UINavigationControllerDelegate
>
// UILabel for displaying the result of the sending the message.

@end


@implementation VEMailView{
    BOOL alreadyOpened;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    alreadyOpened = NO;

}
- (void) viewDidAppear:(BOOL)animated {
[self showMailPicker];

}

- (void)showMailPicker
{

    if ([MFMailComposeViewController canSendMail])

    {
        [self displayMailComposerSheet];
    }

}

- (void)displayMailComposerSheet 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"iOS"];

    NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];

    [picker setToRecipients:toRecipients];

    NSString *emailBody = @" ";
    [picker setMessageBody:emailBody isHTML:NO];

    [self presentViewController:picker animated:YES completion:NULL];
}

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

    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Result: Mail sending canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Result: Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Result: Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Result: Mail sending failed");
            break;
        default:
            NSLog(@"Result: Mail not sent");
            break;
    }
[self dismissViewControllerAnimated:YES completion:nil];
}



@end

Upvotes: 3

Views: 533

Answers (2)

Vyacheslav
Vyacheslav

Reputation: 27211

I found my own solution.

To solve this problem change

[self dismissViewControllerAnimated:YES completion:nil];

into

[self dismissViewControllerAnimated:YES completion:^{[self dismissViewControllerAnimated:YES completion:nil];}];

Thanks Michal for idea.

Upvotes: 1

Michal
Michal

Reputation: 15669

That's because you are making an extra view controller. Create the MFMailComposeViewController in the view controller where the button is. The mail compose controller is a controller by itself. There is a white screen because that is the default view of YOUR VEMailView. Get rid of that and put these methods:

- (void)showMailPicker;
- (void)displayMailComposerSheet;

in the view controller with the button. Also make it the delegate.

Upvotes: 3

Related Questions