MFMailComposeViewController在iOS6 ARC中崩溃



我从我的自定义类(不是viewController)呈现MFMailComposeViewController。在iOS5中,它工作得很好,但在iOS6中,它在呈现撰写表后立即崩溃。我发现问题 dealloc方法在呈现视图后被调用,所以self正在deallocate 。由于这个原因,邮件编写器无法在self上调用委托方法,因此它正在崩溃。我没有找到解决方法。我使用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中已被弃用。

你需要使用

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

mailer将在方法体完全执行后自动释放。

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

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

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

[编辑建议]

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

我也遇到过类似的情况。最后,我将"自定义类"转换为一个单例类。

#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:...];

最新更新