Crash On MFMailComposeViewController For iPad

2019-07-28 03:31发布

问题:

- (void) mailshareClick:(UIButton *)sender
{
    NSString  *_message = @"wait for set up Mail";
    [self waitForWhile:_message];
    if ([MFMailComposeViewController canSendMail]){
        [NSThread detachNewThreadSelector:@selector(mailFunction) toTarget:self withObject:nil];
    }
    else {
        _message = @"Please set Mail account";
        [self remove:_message];
    }
}
- (void) mailFunction
{
    NSData *data = nil;
    if ([self.files.imageArr count]>0) 
    {
        XXImage *single = [self.files.imageArr objectAtIndex:0];
        UIImage *image = [[SDImageCache sharedImageCache] imageFromKey:[[single.imagearray objectAtIndex:0] columnImage]];
        data = [UIImageJPEGRepresentation(image, 1.0f) retain];
    }
    [self performSelectorOnMainThread:@selector(mailFinished:) withObject:data waitUntilDone:YES];    
    [data release];
}
- (void) mailFinished:(NSData *)_data
{
    if ([MFMailComposeViewController canSendMail]){
        NSData *data = [_data retain];
        MFMailComposeViewController   *message = [[MFMailComposeViewController alloc] init];
        //Title
        [message setSubject:self.files.title];
        //Body
        [message setMessageBody:@"111" isHTML:YES];

        [message setToRecipients:[NSArray arrayWithObject:@"mail"]];
        //Content
        if (data != nil) {
            NSString *picStr = [[NSString alloc] initWithFormat:@"%@%@",OutsideWebsite_Normal,self.files.middlePicPath];
            [message addAttachmentData: data mimeType: @"" fileName:picStr];
            [picStr release];
            [data release];
        }
        message.mailComposeDelegate = self;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            message.modalPresentationStyle = UIModalPresentationFormSheet;
        }
        [self presentModalViewController:message animated:YES];

        [self remove:@"Set up Ok"];
        [message release];
    }
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result) {
        case MFMailComposeResultSent:
        {
            NSLog(@"MFMailComposeResultSent");
            break;
        }
        case MFMailComposeResultSaved:
        {
            NSLog(@"MFMailComposeResultSaved");
            break;
        }
        case MFMailComposeResultFailed:
        {
            NSLog(@"MFMailComposeResultFailed");
            break;
        }
        case MFMailComposeResultCancelled:
        {
            NSLog(@"MFMailComposeResultCancelled");
            break;
        }
        default:
            break;
    }
    [self performSelector:@selector(delayDismissModalView) withObject:nil afterDelay:1];
}
-(void)delayDismissModalView
{
    [self dismissModalViewControllerAnimated:YES];
}

After invoke a few times the email method , there will be

  • [MFSearchResultsViewController hash]: message sent to deallocated instance Or

  • [MFMailComposeViewController hash]: message sent to deallocated instance

crash.

As you think of that, what is the MFSearchResultsViewController function.

Whether it can solve the problem,please give me a hand.

回答1:

You have to setDelegate for the mail controller. If you set the delegate to "self", make sure that the "self" has <MFMailComposeViewControllerDelegate>. If all these can't make it work, make sure you didn't call presentModalViewController twice.