在iOS6 ARC中MFMailComposeViewController崩溃

时间:2013-06-12 04:43:13

标签: ios objective-c mfmailcomposeviewcontroller

我从我的自定义类(不是viewController)中呈现MFMailComposeViewController。在iOS5中它工作正常,但在iOS6中,它在呈现撰写表之后立即崩溃。我发现问题 在呈现视图后调用dealloc方法,因此self会解除分配 。由于这个mailcomposer无法调用自己的委托方法,所以它崩溃了。我没有得到解决方案。我正在使用 ARC 如何阻止self解除分配,直到委托方法被调用?

  -(void)shareOnViewController:(UIViewController *)viewController  completion:(ShareCompletionHandler)completion
{

  if ([MFMailComposeViewController canSendMail]) {

    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    mailer.mailComposeDelegate = self;
    mailer.modalPresentationStyle = UIModalPresentationPageSheet;
    [mailer setSubject:[self.userInfo objectForKey:@"title"]];

    NSData *imageData = UIImagePNGRepresentation([self.userInfo objectForKey:@"image"]);
    if (imageData) {
        [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"AttachedImage"];
    }


    NSURL *emailBody = [self.userInfo objectForKey:@"url"];
    if (![emailBody isEqual:@""]) {
        [mailer setMessageBody:[emailBody absoluteString] isHTML:NO];
    }

    [viewController presentModalViewController:mailer animated:YES];

   } else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unable to send mail"
                                                    message:@"Device is not configured to send mail"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
   }

self.completionHandler = completion;

}

3 个答案:

答案 0 :(得分:1)

据我所知,iOS 6.0中不推荐使用presentModalViewController方法。

相反,您需要使用

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion 

否则你能显示崩溃日志吗?

答案 1 :(得分:0)

  if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc]
                                                     init];

    NSData *imageData = UIImagePNGRepresentation(image);
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:subject];
    NSArray * recipents = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",NSLocalizedString(@"client_email", @"")],nil];

    [mailComposer setToRecipients:recipents];

    [mailComposer addAttachmentData:imageData mimeType:@"image/png" fileName:[NSString stringWithFormat:@"imageProfile-%@-%@",someId,lastname]];



    mailComposer.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    [vc presentViewController:mailComposer animated:YES completion:nil];
}
else
{
    UIAlertView *tmp = [[UIAlertView alloc]
                        initWithTitle:@"Email Account Not Found"
                        message:@"You need to setup an Email Account!"
                        delegate:self
                        cancelButtonTitle:nil
                        otherButtonTitles:@"Ok", nil];

    [tmp show];
}

答案 2 :(得分:0)

可能的根本原因之一是ARC。

 MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
方法体完全执行后,

邮件程序将自动释放。

你可以创建一个引用来保持对象,以避免ARC在它准备好之前释放它。

@property (nonatomic, strong) MFMailComposeViewController * composer;
....


self.composer = [[MFMailComposeViewController alloc] init];
...
[viewController presentViewController:self.composer animated:YES];

[编辑建议]

抱歉,我错过了您从其他自定义类调用该方法的问题的第一部分。

我也遇到了类似的情况。最后,我将“Custom Class”转换为Singleton类。

#pragma mark Singleton Methods

+ (id)sharedInstance {
    static CustomClass *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

这将确保课程得到很好的保留。但这取决于CustomClass的用法和设计。只是为了分享我的所作所为。

[[CustomClass sharedInstance] shareOnViewController:vc completion:...];