iPhone: How to Close MFMailComposeViewController?

2019-05-06 23:45发布

问题:

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

回答1:

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.