20

在我的应用程序中,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];
}

似乎很明显,一切都正确链接,因为电子邮件视图控制器工作正常。有什么我遗漏的东西可能是配置明智的吗?

4

1 回答 1

49

你检查过+[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 不这样做。

于 2010-08-17T03:15:32.950 回答