1

MFMailComposeViewController我从我的自定义类(不是 viewController)中呈现。在 iOS5 中它工作正常,但在 iOS6 中它在呈现撰写表后立即崩溃。我发现了在呈现视图后调用 dealloc 方法的问题,因此 self 正在解除分配。由于这个 mailcomposer 不能调用 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;

}
4

3 回答 3

1

据我说,该presentModalViewController方法在 iOS 6.0 中已弃用。

相反,您需要使用

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

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

于 2013-06-12T05:00:28.370 回答
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];
}
于 2013-06-12T13:50:55.800 回答
0

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

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

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

您可以创建一个引用来保存对象,以避免 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:...];
于 2014-07-08T11:51:45.813 回答