按“删除草稿”时MFMailComposeViewController崩溃

时间:2013-04-28 20:16:33

标签: xcode email mfmailcomposeviewcontroller

我有一个调用MFMailComposeViewController的按钮。有时,当我点击"删除草稿"时,应用程序崩溃但有时它会正确地关闭视图。我可以'明白为什么。这是我的代码:

- (IBAction)openEmail:(id)sender {
if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    composer.mailComposeDelegate = (id)self;
    NSArray *myEmail = [[NSArray alloc] initWithObjects:@"me@gmail.com", nil];
    [composer setToRecipients:myEmail];
    [self presentViewController:composer animated:YES completion:nil];
    [[composer navigationBar] setTintColor:[UIColor colorWithRed:0.843 green:0.435 blue:0.435 alpha:1]];
}
else {

   }
}

和这个方法:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");

        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");

        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail send");

        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail failed");

        break;
    default:
        NSLog(@"Mail not sent.");

        break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

我以前也经历过这种情况,代码中潜在的问题区域可能是这一行:

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

可能在调用MFMailComposeViewControllerDelegate方法时,您的composer对象被释放。相反,尝试在强大的属性上启动MFMailComposeViewController,如下所示:

self.composer = [MFMailComposeViewController new];

您可以在委托方法采取行动后将self.composer设置为nil以释放MFMailComposeViewController

答案 1 :(得分:0)

你试过了吗?

[self presentModalViewController:_picker animated:YES];

注意'MODAL'?

还有:

[self dismissModalViewControllerAnimated:YES];