その部分が機能しているアプリからメールを送信できるようにしたいです。問題は、電子メールの送信をキャンセルすると (これは現在テストしているものです)、電子メールの部分は閉じられますが、閉じられないように見える黒い画面が残ります。
これが私が持っているものです。メール部分を処理するクラスを作成しました。
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];
}
このすべてを実行すると、電子メール プログラムが開き、電子メールの操作を行うことができます。[キャンセル] を選択してから [下書きを削除] を選択すると、電子メール プログラムが削除されます。ただし、黒い画面が残っているので、上部のナビゲーション バーから [戻る] を選択して前のビューに戻ることができます。
電子メールが送信またはキャンセルされたときに、電子メール プログラムを表示するビューにアプリが戻るようにしたいだけです。私が見逃しているのは単純なものだと確信しています。