0

MFMailComposeViewController在我的 demp 应用程序中实现。但是,由于某些原因,在我触摸以在电子邮件文本字段中添加文本后,我的应用程序崩溃了。但是在不添加任何文本的情况下发送效果很好。

我的 Xcode 没有显示太多。这是我得到的:

在此处输入图像描述

我已经在电子邮件文本字段中设置了初始文本。也许问题就在这里?请求任何代码,我很乐意将其包含在内。

更新: 这里有两种方法,其中第一种方法openMail在触摸时触发UIButton

- (IBAction)openMail:(id)sender
{
    if ([MFMailComposeViewController canSendMail])
    {

    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                        message:@"Your device doesn't support the composer sheet"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

        mailer.mailComposeDelegate = self;

        [mailer setSubject:@"Feedback from Demo App user"];

        NSArray *toRecipients = [NSArray arrayWithObjects:@"myEMAIL@hotmail.com", @"myEMAIL2@gmail.com", nil];
        [mailer setToRecipients:toRecipients];

        NSString *emailBody = @"Happy to hear your feedback!";
        [mailer setMessageBody:emailBody isHTML:NO];

        [self presentModalViewController:mailer animated:YES];

        [mailer release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                        message:@"Your device doesn't support the composer sheet"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
    }

}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }

    // Remove the mail view
    [self dismissModalViewControllerAnimated:YES];
}
4

2 回答 2

0

可能您正在使用 ARC,并且您没有指向您的强大指针,MFMailComposeViewController并且在显示之后,ARC 正在释放它。

利用

@property (nonatomic,stron) MFMailComposeViewController *mail;

当你初始化 MFMailComposeViewController 时:

MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
self.mail = mailer;
于 2012-09-01T19:56:53.377 回答
0

您的代码中是否还有这一行:

[mailer release];

如果是这样,那就是问题所在。如果没有,请更新我们的代码示例,使其与您现在使用的完全一致。

于 2012-09-01T23:06:02.693 回答