我希望能够从我的应用程序发送一封电子邮件,其中我已经让那部分工作。问题是当我取消发送电子邮件(这是我现在正在测试的)时,电子邮件部分被关闭,但我留下了一个我似乎无法关闭的黑屏。
所以这就是我所拥有的。我创建了一个类来处理电子邮件部分:
MailViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface MailViewController : UIViewController <MFMailComposeViewControllerDelegate>
@end
实现如下:
#import "MailViewController.h"
@interface MailViewController ()
@end
@implementation MailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
[mailer setMailComposeDelegate:self];
[mailer setSubject:@"Subject"];
NSMutableArray *toArray = [[NSMutableArray alloc] initWithObjects:@"email@gmail.com", nil];
[mailer setToRecipients:toArray];
[self presentViewController:mailer animated:YES completion:nil];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failuer" message:@"This device is unable to send emails" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result){
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled: you cancelled the operation and no email was sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail failed");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
default:
NSLog(@"Mail not sent");
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}
@end
所有这些都是从另一个视图显示的,如下所示:
-(IBAction)sendMessage:(id)sender{
NSLog(@"Going to send an email....");
MailViewController *mail = [[MailViewController alloc] init];
[self.navigationController pushViewController:mail animated:YES];
}
所以当我运行所有这些时,电子邮件程序会打开,我可以处理电子邮件。我可以选择取消,然后选择删除草稿,然后删除电子邮件程序。但是,我留下了一个黑屏,然后我可以从顶部导航栏中选择返回以返回到上一个视图。
我只是希望应用程序在发送或取消电子邮件(或其他)时返回显示电子邮件程序的视图。我敢肯定这是我想念的简单的东西。