我有一个应用程序,允许用户从他们的iPhone发送测试电子邮件。我的应用程序调用一个方法来激活撰写邮件功能,如下所示:
-(void)displayComposerSheet
{
//set up a way to cancel the email here
//picker is an instance of MSMailComposeViewController already declared in the .h file
[picker setSubject:@"Test Mail"];
// Set up recipients
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"Icon" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"Icon"];
// Fill out the email body text
NSString *emailBody = @"This is a test mail.";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
NSLog(@"mail is working");
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
emailLabel.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
emailLabel.text = @"Mail sending canceled.";
break;
case MFMailComposeResultSaved:
emailLabel.text = @"Mail saved.";
break;
case MFMailComposeResultSent:
{
emailLabel.text = @"Mail sent.";
NSLog(@"It's away!");
UIAlertView *emailAlertView = [[UIAlertView alloc] initWithTitle:@"Sent!" message:@"Mail sent successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[emailAlertView show];
[self dismissModalViewControllerAnimated:YES];
[self.navigationController popViewControllerAnimated:YES];
}
break;
case MFMailComposeResultFailed:
{
emailLabel.text = @"Mail sending failed.";
}
break;
default:
{
emailLabel.text = @"Mail not sent.";
}
break;
}
}
我的问题是,当撰写电子邮件功能处于活动状态时,我无法退出此功能并返回我的应用程序。摆脱这种情况的唯一方法是实际进行并发送消息。导航栏左上角有一个默认的“取消”栏按钮,点击后会给我三个选项:“删除草稿”,“保存草稿”和“取消”。如果我选择“删除草稿”,它除了将我返回到撰写邮件屏幕外什么都不做。有没有办法允许用户在启动撰写邮件功能后允许用户返回应用程序,但在发送之前?有没有办法在“取消”栏按钮中添加额外的功能来实现这一目标?
感谢所有回复的人。
答案 0 :(得分:3)
您必须使用MFMessageComposeViewControllerDelegate
方法实施- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
。
您将在此方法中关闭您的消息视图。
编辑:我与MFMailComposeViewControllerDelegate
混淆,但其目的与MFMessageComposeViewControllerDelegate
相同
答案 1 :(得分:0)
使用...didFinishWithResult:
方法查看您自己的代码:
case MFMailComposeResultCancelled:
emailLabel.text = @"Mail sending canceled.";
break;
case MFMailComposeResultSaved:
emailLabel.text = @"Mail saved.";
break;
case MFMailComposeResultSent:
{
emailLabel.text = @"Mail sent.";
NSLog(@"It's away!");
UIAlertView *emailAlertView = [[UIAlertView alloc] initWithTitle:@"Sent!" message:@"Mail sent successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[emailAlertView show];
[self dismissModalViewControllerAnimated:YES];
[self.navigationController popViewControllerAnimated:YES];
}
当结果为MFMailComposeResultSent
时,您正在关闭模态视图控制器并弹出导航堆栈,这会导致邮件撰写视图控制器消失,并弹出堆栈以删除显示的视图控制器撰写视图控制器。但是,当结果为MFMailComposeResultCancelled
时,您只需设置一些标签的文本。 MFMailComposeResultSaved
也是如此。如果您希望撰写视图控制器在用户取消或保存时消失,则需要关闭这些情况下的消息撰写视图控制器。
答案 2 :(得分:0)
您应该在视图控制器中添加此方法
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
[self dismissViewControllerAnimated:YES completion:nil];
}
希望这个可以帮助一个人。
答案 3 :(得分:0)
设置MFMailComposeViewController的委托 MFMailComposeViewController * mailcomposer = [[MFMailComposeViewController alloc] init];
mailcomposer.mailComposeDelegate = self;
并使用此委托方法 - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)结果错误:(NSError *)错误