MFMessageComposeViewController alloc返回nil

Lee*_*e 18 iphone objective-c ios4 mfmailcomposeviewcontroller

在我的应用程序中,MFMailComposeViewController工作正常,但创建MFMessageComposeViewController的新实例失败.

以下是两者的代码:

-( IBAction)sendSMS: (id)sender
{
 MFMessageComposeViewController *picker = [[[MFMessageComposeViewController alloc] init] autorelease];
 picker.messageComposeDelegate = self;

 NSArray *toRecipients = [NSArray arrayWithObject: cell.currentTitle ]; 

 picker.recipients = toRecipients;

 [self presentModalViewController:picker animated:YES];
}

-( IBAction)sendEmail: (id)sender
{
 MFMailComposeViewController *picker = [[[MFMailComposeViewController alloc] init] autorelease];
 picker.mailComposeDelegate = self;

 NSArray *toRecipients = [NSArray arrayWithObject: email.currentTitle ]; 

 [picker setToRecipients:toRecipients];

 [self presentModalViewController:picker animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

由于电子邮件视图控制器工作正常,所以一切都正确链接似乎很明显.是否有一些我错过的配置明智?

tc.*_*tc. 47

你检查过+[MFMessageComposeViewController canSendText]吗?

MFMessageComposeViewController类参考,

在显示消息组合视图之前,请调用canSendText类方法以确保正确配置用户的设备.如果canSendText方法返回NO,请不要尝试显示消息组合视图.如果SMS传送不可用,您可以通知用户或只是禁用应用程序中的SMS功能.

从iOS 5开始,您可以注册以通知通知发送短信的可用性的变化MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification.

它可能返回的原因nil:

  • 设备未运行iOS 4.
  • 设备是未启用iMessage的iPod Touch/iPad.
  • 没有SIM卡?(该视图现在显示在iOS 6中;应用程序未收到消息发送失败的通知.)
  • "设备"实际上是模拟器.(也许这也适用于iOS 6.)

同样,在没有启用邮件帐户时[[MFMailComposeViewController alloc] init]返回nil(您可以通过在"设置"中禁用帐户来快速测试),但也会显示"未配置邮件帐户"警报.MFMessageComposeViewController不会这样做.