我正在开发一个应用程序,要求是在UIAlertView的按钮上打开电子邮件编辑器。
电子邮件的邮件正文中的邮件是从UITextView复制的。我正在使用以下代码片段:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0)
{
// opening message composer
}
else
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Test mail"];
[picker setMessageBody:messageBody.text isHTML:YES];
[self presentViewController:picker animated:YES completion:NULL];
}
}
// mail compose delegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
但问题是我收到错误,说应用程序试图在目标上显示一个nil模态视图控制器。我们如何在ios 7中打开默认邮件编辑器?
答案 0 :(得分:70)
根据Apple,您应该检查 MFMailComposeViewController 是否能够在发送之前发送您的邮件
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Test mail"];
[picker setMessageBody:messageBody.text isHTML:YES];
[self presentViewController:picker animated:YES completion:NULL];
}
答案 1 :(得分:9)
在设备设置中忘记邮件帐户配置也可能导致此错误。重新检查您的设备中是否配置了邮件帐户。
答案 2 :(得分:7)
Swift 4版
guard MFMailComposeViewController.canSendMail() else {
print("Mail services are not available")
return
}
sendEmail()