【问题标题】:MFMailComposeViewController is dismissing immediately when appearMFMailComposeViewController 出现时立即关闭
【发布时间】:2016-04-12 11:44:25
【问题描述】:

MFMailComposeViewController 出现时立即关闭

- (IBAction)btnContactPressed:(id)sender
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        [mailer setSubject:@"Feedback"];

        NSArray *toRecipients = [NSArray arrayWithObjects:@"salimullah240@gmail.com", nil];
        [mailer setToRecipients:toRecipients];

        [self presentViewController:mailer animated:YES completion:nil];
    } 
    else    
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                        message:@"Your device doesn't support the composer sheet"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }

    // Remove the mail view
    [self dismissViewControllerAnimated:YES completion:nil];
}

【问题讨论】:

  • 你在模拟器中测试?
  • 你是在模拟器还是设备上测试?
  • 签入设备可能会起作用
  • 自 iOS 8 以来,模拟器上存在一个错误,该错误会破坏此问题,设备应该可以正常工作
  • 你确定你没有在其他地方打电话给dismissViewControllerAnimated:completion:吗?

标签: ios objective-c xcode7 mfmailcomposeviewcontroller


【解决方案1】:

您的代码是正确的。我试过了,效果很好。 MFMailComposeViewController 组件不能在 iOS simulator 中测试,只能在 device 中测试。

如果您在Apple Developer Forums 中查看此Thread,则问题在Apple Bug Report 中有一张票,但仍然没有任何修复。

另外,请确保您正在导入:

#import <MessageUI/MFMailComposeViewController.h> 

并添加delegate:

@interface ViewController () <MFMailComposeViewControllerDelegate>

【讨论】:

  • 对您的代码的额外提示是:UIAlertView 已弃用。使用UIAlertControllersee more
猜你喜欢
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 2011-11-14
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多