在iOS6 ARC中,MFMailComposeViewController崩溃

Iam从我的自定义类(不是一个viewController)呈现MFMailComposeViewController 。 在iOS5中,它工作正常,但在iOS6中,它呈现组合表后立即崩溃。 我发现dealloc方法在呈现视图后被调用的问题,所以自我正在释放 。 由于这个mailcomposer不能调用委托方法,所以它崩溃了。 我没有得到一个解决scheme。 我正在使用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; } 

据我presentModalViewController在iOS 6.0中不推荐使用presentModalViewController方法。

相反,你需要使用

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

否则,你可以显示崩溃日志?

  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]; } 

其中一个可能的根源是ARC。

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

一旦方法体被完全执行, 邮件程序将被自动释放。

您可以创build一个引用来保存该对象,以避免ARC在准备好之前将其释放。

 @property (nonatomic, strong) MFMailComposeViewController * composer; .... self.composer = [[MFMailComposeViewController alloc] init]; ... [viewController presentViewController:self.composer animated:YES]; 

[编辑build议]

对不起,我错过了从另一个自定义类调用方法的问题的第一部分。

我也遇到过类似的情况。 最后,我将“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; } 

这将确保class级得到很好的保留。 但是这取决于CustomClass的用法和devise。 只是为了分享我所做的。

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