You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

未设置邮件账户时App随机崩溃且偶现错误弹窗,附代码求技术支持

解决未设置邮件账户时App崩溃的问题

Hey Catherine, let's figure out why your app is crashing randomly when there's no email account set up on the device. Looking at your code snippet, I can spot a couple of potential issues right away:

1. 未完成的AlertController代码

Your else block cuts off mid-creation of the UIAlertController — this incomplete code is almost certainly causing crashes or unexpected behavior. You need to fully initialize the alert, add at least one action, and then present it properly.

2. 其他潜在崩溃点

  • Nil attachment data: If myData is nil, calling addAttachmentData: could trigger a crash. Always validate your data before attaching it.
  • Missing delegate implementation: Not handling the MFMailComposeViewControllerDelegate can lead to memory leaks or unexpected behavior when the mail controller is dismissed.

修正后的完整代码

Here's the fixed version of your code that addresses these issues:

// 首先确保遵循MFMailComposeViewControllerDelegate
@interface YourViewController () <MFMailComposeViewControllerDelegate>
@end

@implementation YourViewController

- (void)sendEmailWithPDF {
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
        mc.mailComposeDelegate = self;
        
        // 先检查附件数据是否有效
        if (myData != nil) {
            [mc addAttachmentData:myData mimeType:@"application/pdf" fileName:@"myPdf2.pdf"];
        }
        
        [self presentViewController:mc animated:YES completion:NULL];
    } else {
        // 完整创建AlertController并添加操作
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"无法发送邮件"
                                                                       message:@"请先在设备的“设置”中配置邮件账户"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil];
        [alert addAction:okAction];
        
        [self presentViewController:alert animated:YES completion:NULL];
    }
}

// 实现邮件控制器的代理方法,确保正确关闭
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [controller dismissViewControllerAnimated:YES completion:NULL];
}

@end

额外排查建议

  • Test your app on a device without an email account set up to confirm the fix works.
  • Check if myData is being properly populated before reaching this code — if it's nil unexpectedly, trace back to where it's generated to fix that issue.
  • Ensure the view controller (self) you're presenting from is still active (not dismissed or deallocated) when this code runs.

内容的提问来源于stack exchange,提问作者Catherine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:03:40