在iOS5中关闭dismissModalViewControllerAnimated时MFMailComposeViewController崩溃

Gau*_*rav 4 iphone presentmodalviewcontroller ios mfmailcomposeviewcontroller

我在使用conMail时使用MFMailComposeViewController来提供Mail功能,但是发送邮件后或当我想取消邮件时,它将崩溃。

下面是我的代码:

(IBAction)FnForPlutoSupportEmailButtonPressed:(id)sender {
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

        mailer.mailComposeDelegate = self;

        [mailer setSubject:@"Need help from Pluto support team"];

        NSArray *toRecipients = [NSArray arrayWithObjects:@"support@myplu.to",nil];
        [mailer setToRecipients:toRecipients];


        NSString *emailBody = @"";

        [mailer setMessageBody:emailBody isHTML:NO];

        //mailer.modalPresentationStyle = UIModalPresentationPageSheet;

        [self presentModalViewController:mailer animated:YES];

    }
    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
{
// Notifies users about errors associated with the interface    
switch (result)
    {       case MFMailComposeResultCancelled:
                        break;
                    case MFMailComposeResultSaved:
                        break;
                    case MFMailComposeResultSent:
                        break;
                    case MFMailComposeResultFailed:
                        break;
                    default:
                        break;
    } 
    [self dismissModalViewControllerAnimated:YES];
     }
Run Code Online (Sandbox Code Playgroud)

我已经阅读了所有博客文章,但未找到解决方案,博客文章对此有很好的解释,但是按照此,我没有在viewdidload或viewdidappear中展示我的视图控制器。

我正在获取EXE_BAD_ACCESS,以下是崩溃日志:

**

> #0  0x00000000 in ?? ()
> #1  0x01dc5aa4 in -[UIViewController _setViewAppearState:isAnimating:] ()
> #2  0x01dc5f47 in -[UIViewController __viewDidDisappear:] ()
> #3  0x01dc6039 in -[UIViewController _endAppearanceTransition:] ()
> #4  0x01dd2e7e in -[UIViewController(UIContainerViewControllerProtectedMethods) endAppearanceTransition] ()
> #5  0x01fc8de1 in -[UIWindowController transitionViewDidComplete:fromView:toView:] ()
> #6  0x01da334b in -[UITransitionView notifyDidCompleteTransition:] ()
> #7  0x01da3070 in -[UITransitionView _didCompleteTransition:] ()
> #8  0x01da531b in -[UITransitionView _transitionDidStop:finished:] ()
> #9  0x01d23fb6 in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] ()
> #10 0x01d24154 in -[UIViewAnimationState animationDidStop:finished:] ()
> #11 0x0163bbce in CA::Layer::run_animation_callbacks ()
> #12 0x03664fe4 in _dispatch_client_callout ()
> #13 0x03655997 in _dispatch_main_queue_callback_4CF ()
> #14 0x012c03b5 in __CFRunLoopRun ()
> #15 0x012bf804 in CFRunLoopRunSpecific ()
> #16 0x012bf6db in CFRunLoopRunInMode ()
> #17 0x030f1913 in GSEventRunModal ()
> #18 0x030f1798 in GSEventRun ()
> #19 0x01ce82c1 in UIApplicationMain ()
Run Code Online (Sandbox Code Playgroud)

**

根据针对iOS 5的Apple的更新文档,他们提到:

presentModalViewController:动画化:

向用户展示由给定视图控制器管理的模式视图。(已弃用。请presentViewController:animated:completion:改用。)

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
Parameters
Run Code Online (Sandbox Code Playgroud)

取消接收方提供的视图控制器。(已弃用。请dismissViewControllerAnimated:completion:改用。)

- (void)dismissModalViewControllerAnimated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)

我也尝试过,但仍然崩溃

Dav*_*d H 5

您需要在类中保留对MFMailComposeViewController * mailer的强引用,并且在您将其关闭后可以将该引用为空。问我我怎么知道:-)

@implememtation MyClass
{
    MFMailComposeViewController *mailer;
}
...

(IBAction)FnForPlutoSupportEmailButtonPressed:(id)sender {
{
    if ([MFMailComposeViewController canSendMail])
    {
        /* USE IVAR */mailer = [[MFMailComposeViewController alloc] init];
Run Code Online (Sandbox Code Playgroud)

稍后,当它完全完成后,您只需“ mailer = nil;”。释放它。

编辑:我要做的和建议是对主队列使用一个块来进行发布。如果仅使用“ self.mailer = nil”,则释放将在最终的委托方法完成后发生,并且您可以确定不再使用它。

EDIT2:这种崩溃不会在所有设备上始终发生-我要说的是,在您收到最终的委托方法与其完成工作之间的某种竞争状况。苹果公司并没有说要以一种方式持有参考资料。但是,苹果产品的一般做法是假设您通过一个runLoop借来的任何对象都是“借来的”,此后,如果您要保留参考资料,则必须保留对象。