MFMailComposeViewController不会使用“取消”按钮关闭

时间:2016-12-26 09:44:09

标签: ios objective-c mfmailcomposeviewcontroller

我有一个使用函数子类NSObject的类确实显示了一个MFMailComposeViewController。这是代码:

MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];

mailController.mailComposeDelegate = self;
[mailController setSubject:@"Sample Subject"];
[mailController setMessageBody:@"Here is some main text in the email!" isHTML:NO];
[mailController setToRecipients:@[self.email]];

UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
UINavigationController *navigationController = tabbarController.selectedViewController;
[navigationController.topViewController presentViewController:mailController animated:YES completion:NULL];

这段代码一切正常。问题是当我想要解雇MFMailComposeViewController时。有时候我遇到了崩溃,有时它不会发生任何事情。我已经实现了委托功能:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    UINavigationController *navigationController = tabbarController.selectedViewController;
    [navigationController.topViewController dismissViewControllerAnimated:YES completion:nil];
}

之后我尝试从ViewController直接显示和解除它,一切正常。甚至取消按钮。

我不知道为什么它在我的ViewController类中有效,但在我的NSObject子类中却没有。

当我在日志中看到崩溃时:

-[MFMailComposeInternalViewController _notifyCompositionDidFinish]

2 个答案:

答案 0 :(得分:0)

试试这个,

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{
    // Do your work and dismiss after you are done with it.
    [controller dismissViewControllerAnimated:YES completion:nil];
}

希望有所帮助。

答案 1 :(得分:0)

在你的单身人士课程中尝试这个

    UIViewController *currentViewController;

    - (void)sendEmail:(id) viewController {

 currentViewController=(UIViewController*)viewController;

        NSString * appVersionString =@"";
        NSString *strEmailMessage=@"";
        NSString *strEmailSubject=@"";


        NSArray *toRecipents =@"";



        if ([MFMailComposeViewController canSendMail]) {

            MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
            mc.mailComposeDelegate =viewController;
            [mc setSubject:strEmailSubject];
            [mc setMessageBody:strEmailMessage isHTML:NO];
            [mc setToRecipients:toRecipents];


            [viewController presentViewController:mc animated:YES completion:NULL];


        }
        else{
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" message:@"Please setup email account" delegate:nil cancelButtonTitle:@"cancle" otherButtonTitles:nil];

            [alert show];
        }

    }

像这样设置委托

 mc.mailComposeDelegate =viewController;

解散viewController

 - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        switch (result)
        {
            case MFMailComposeResultCancelled:

                break;
            case MFMailComposeResultSaved:
               break;
            case MFMailComposeResultSent:

                break;
            case MFMailComposeResultFailed:

                break;
        }

        // Close the Mail Interface
        [currentViewController dismissViewControllerAnimated:YES completion:NULL];
    }

我希望这会奏效......