我无法创建MFMailComposeViewController的对象

时间:2011-11-25 08:52:51

标签: objective-c ios mfmailcomposeviewcontroller

我无法创建MFMailComposeViewController的对象。你能救我吗?

我尝试了2种方法但它不起作用:

MFMailComposeViewController *mf = [MFMailComposeViewController new];

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

有人知道发生了什么吗? 感谢。

P.S。它在带有ios 4.3.3的iPad 2上不起作用,但它适用于带有ios 4.3.2的iPad 1

3 个答案:

答案 0 :(得分:4)

检查[MFMailComposeViewController canSendMail](文档已链接)是否返回YES。

我怀疑它不是,在你的情况下。

如果确实返回yes,那么在执行alloc / init后,“mf”是否设置为null?

还要确保将MessageUI.framework添加到项目中。 In case you need it, here's a tutorial

答案 1 :(得分:2)

我使用第二种格式:

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

确保:

  • 您已将MessageUI.framework添加到项目
  • 您已将#import <MessageUI/MessageUI.h>添加到类标题文件
  • 您声明MFMailComposeViewControllerDelegate以及您需要的任何相关方法

如果设备在您创建时没有设置任何邮件帐户,稍后再拨打[self presentModalViewController:mf animated:YES];,那么您将获得Apple警报视图,通知您情况。

答案 2 :(得分:1)

1)将MessageUI.framework添加到您的项目

2)将#import导入到你的视图控制器

3)声明MFMailComposeViewControllerDelegate方法

        MFMailComposeViewController *mail = [[MFMailComposeViewController alloc]init];
        mail.mailComposeDelegate = self;
        [mail setToRecipients:[NSArray arrayWithObjects:@"mail id", nil]];
        [mail setSubject:@"Contact Us"];
        [mail setMessageBody:@"If U want any information u can send the mail <div> " isHTML:YES];
        [self presentViewController:mail animated:YES completion:nil];
        [mail release];