Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: How to Close MFMailComposeViewController?

I'm having difficulties closing an email message that I have raised.

The email opens nicely, but once it is opened it will not close as the mailComposeController:mailer didFinishWithResult:result error:error handler never gets invoked.

As far as I can tell I have all the bits in place to be able to do this.

Anyone any ideas of what I can look at?

Here is how I raise the email:

-(IBAction)emailButtonPressed 
{

NSString *text = @"My Email Text";

 MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
 mailer.delegate = self;

 [mailer setSubject:@"Note"];
 [mailer setMessageBody:text isHTML:NO];
 [self presentModalViewController:mailer animated:YES];
 [mailer release];
}

and later in the class I have this code to handle the close (but it never gets called):

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

My header file is defined as:

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface myViewController : UIViewController <UIActionSheetDelegate, UIAlertViewDelegate, MFMailComposeViewControllerDelegate, UINavigationControllerDelegate>

Thanks

Iphaaw

like image 935
iphaaw Avatar asked Dec 21 '22 21:12

iphaaw


1 Answers

You are setting the delegate wrong, the delegate property in MFMailComposeViewController is called mailComposeDelegate, so it should be:

mailer.mailComposeDelegate = self;

Another possible error I can see is calling dismissModalViewControllerAnimated: on mailer - you should send this message to the view controller who presented the mail interface - self in this case:

[self dismissModalViewControllerAnimated:YES];

I wrote "possible error" because it might actually work if iOS routes the message through responder chain, anyway - the documentation says it should be send to presenter.

like image 155
Michal Avatar answered Jan 06 '23 20:01

Michal