MFMailComposeViewController在iOS 8中无法显示

9
当我运行Xcode 6并使用iOS 8模拟器时,我试图加载MFMailComposeViewController,但它没有出现。相反,我收到了这个错误...
“警告:尝试在已经呈现(null)的ViewController:0x7baf6000上呈现MFMailComposeViewController:0x7c30c400。”
这完全相同的代码已经工作了将近一年了,没有改变。为了确保,我在Xcode 6中使用iOS 7.1模拟器运行我的应用程序,它像以前一样完美地工作。
有人知道如何在iOS 8中显示MFMailComposeViewController视图吗?这似乎是一个简单的任务,我相信这是一个容易解决的问题。
PS.不需要发布,因为这是标准代码,但这是代码...
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

picker.mailComposeDelegate = self;

//Current Date
NSDateFormatter *formatter = [[NSDateFormatter alloc]  init];
NSDate  *date = [NSDate date];
//Ensure UTC DATE
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"MM/dd/yyyy 'at' HH:mm:ss UTC"];
NSString *myString = [formatter stringFromDate:date];

NSString *emailBody = [NSString stringWithFormat:@"Feedback time at %@.<br><br>Name:", myString];
[picker setMessageBody:emailBody isHTML:YES];

NSString *subject, *emailAddress;

[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"MM/dd/yyyy"];
subject = [NSString stringWithFormat:@"Feedback"];

//Set the Subject
[picker setSubject:subject];

emailAddress = @"xxx@example.com";

//Set the Receivers
NSArray *myReceivers = [[NSArray alloc] initWithObjects:emailAddress, nil];
[picker setToRecipients:myReceivers];

//It saves the file name as the same as Subject here!
picker.navigationBar.barStyle = UIBarStyleDefault;//UIBarStyleBlack;
[self presentViewController:picker animated:YES completion:^{}];
9个回答

6

我在iOS 8上遇到了类似的问题,在iOS 7及其之前的版本上正常工作。

当用户按下UIActionSheet中的按钮时,我会呈现我的MFMailComposeViewController。因此,在以前的实现中,我将邮件组合器放在UIActionSheetDelegate函数中,并显示MFMailComposeViewController

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

这个函数名很直接。

但是,可能由于一些UI动画冲突,同时显示MFMailComposeViewController和关闭UIActionSheet会导致MFMailComposeViewController无法显示。

  • 解决方法

我的解决方法是将MFMailComposeViewController显示在UIActionSheetDelegate函数中:

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

UIActionSheet动画完成后调用的方法。

希望这能帮到有同样问题的人。


5

我曾经遇到了类似的问题,但没有看���同样的提示信息。然而,在iOS 8上,我的邮件撰写器无法显示。我从UIPopover上的一个按钮启动它。我会显示邮件撰写器,然后在几行代码之后,我调用:

    [myPopover dismissPopoverAnimated:YES];

在StackOverflow上的另一个帖子中指出,问题可能与另一个视图控制器处于活动状态有关。因此,我将关闭弹出窗口的代码移到了调用显示邮件编辑器的代码之前,并且取消了动画效果:

    [aboutPopover dismissPopoverAnimated:NO];

现在它可以工作了。

那就是它的本质。我也是从UIPopover中展示它的,我在打开MailComposer之前添加了一行代码来关闭它,然后它就起作用了!谢谢 - Airtower

1

我不知道为什么这个方法有效。在调用此方法之前,我没有呈现任何其他的视图控制器,但是如果我先dismiss视图控制器,它就有效了...

[self dismissViewControllerAnimated:YES completion:^{    [self presentViewController:picker animated:YES completion:^{}];
}];

0

有相同的问题,似乎SDK8.0在IOS8中将MFMailComposeController作为一个popOver构建,因此您必须关闭任何其他popOver。以下是它的工作示例。

-(void)sendMail{
    [[[self fatherController] sharePopOver] dismissPopoverAnimated:YES];
    MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
    [controller setMailComposeDelegate:[self fatherController]];
    [[self fatherController] presentViewController:controller animated:YES completion:NULL];
}

此问题仅发生在iPad上,因为iPhone / iPod中没有弹出窗口。 - user2387149

0
在弹出窗口函数中,只需这样做。
    @IBAction func exportAsMail(sender: AnyObject) {

    NSNotificationCenter.defaultCenter().postNotificationName("exportAsMail", object: nil)

    self.dismissViewControllerAnimated(false, completion: nil)
   ///****animated have to be false not true


}

0

MFMailComposeViewController 在 iOS 8 模拟器上已经无法使用。

如果您尝试在实际设备上运行,请尝试仅呈现选择器而不进行任何其他更改,如下所示:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
[self presentViewController:picker animated:YES completion:nil];

值得一提的是,还应该检查[MFMailComposeViewController canSendMail]返回的内容


我尝试在设备上运行,但没有成功。 - Airtower
我刚刚在另一台iPad上尝试了一遍,但没有成功。 - Airtower
@Airtower [MFMailComposeViewController canSendMail] 返回什么?你尝试过运行上面的两行代码吗?控制台输出中是否有任何消息?你是否遇到任何崩溃?你是否导入了MessageUI? @import MessageUI; - liamnichols

0

在UIActionSheet上也遇到了同样的问题。当UIActionSheet存在时(仅限iOS 8),MFMailComposeViewController不会显示出来,即使你没有使用动画将其关闭。等待1秒钟可耻地解决了这个问题。因此,我在新设备上使用UIAlertController(在iOS 8中引入),而在旧设备上使用UIActionSheet。现在,由于苹果公司,我的代码变得又丑又长。


1
嗨,我通过使用UIActionSheet解决了问题。 我将MFMailComposeViewController显示放在另一个UIActionSheetDelegate回调函数-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex中。 - Charlie Hung

0

MailComposer在模拟器上无法工作,但在设备(iOS 8)上可以正常使用。


-1
这是我用来显示邮件组合器的代码,在我的Xcode 6.0.1版本和我的设备(iOS8的iPhone 5)上运行良好:
-(void) showMailComposer {
    if(![MFMailComposeViewController canSendMail] ) {
        NSLog(@"Cannot send mail\n%s", __PRETTY_FUNCTION__) ;
        return ;
    }

    // Email Subject
    NSString *emailTitle = @"Test Email";
    // Email Content
    NSString *messageBody = @"iOS programming is so fun!";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"default@gmail.com"];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];

    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];
}

我尝试复制并粘贴这段完全相同的代码并使用它,但是我得到了完全相同的错误。 - Airtower
这可能是一个冒险,但你有最新版本的Xcode吗? - ElectrikSheep

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接