iOS:在呈现MFMailComposerViewController时崩溃

时间:2014-07-19 10:45:35

标签: ios mfmailcomposeviewcontroller presentviewcontroller

我只有一小段代码可以显示包含某些细节的邮件视图,并且在调用时

 [self presentViewController:controller animated:YES completion:NULL];

应用程序崩溃,说Thread 1: breakpoint 1.1

我不确定导致这次崩溃的原因。我正在提供以下代码。

NSString* encryptedText = [self getEncryptedText:noteText.text :cipherField.text];

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; controller.mailComposeDelegate = (id)self;

[controller setSubject:titleField.text];
[controller setMessageBody:encryptedText isHTML:NO];

[self presentViewController:controller animated:YES completion:NULL];

由于

2 个答案:

答案 0 :(得分:3)

在创建MFMailComposeViewController之前,请务必通过添加

检查用户是否在其设备上设置了邮件
if ([MFMailComposeViewController canSendMail]) {

    //Place your code here to create the controller and present

}

如果您尝试在尚未设置邮件的设备上显示MFMailComposeViewController,则该应用会崩溃。添加此条件将检测到此情况。

希望这会有所帮助......

答案 1 :(得分:1)

试试这段代码。

if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@""]];
        [composeViewController setSubject:@"Hi Test"];
        [composeViewController setMessageBody:@"Hello Test" isHTML:YES];

        [self presentViewController:composeViewController animated:YES completion:nil];

    }