2010-08-17 68 views
14

在我的应用程序中,MFMailComposeViewController工作正常,但创建MFMessageComposeViewController的新实例失败。MFMessageComposeViewController alloc返回无

下面是两个代码:

-(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]; 
} 

它似乎很明显,一切都正确连接,因为电子邮件视图控制器正常工作。有没有我缺少的配置明智?

回答

42

你检查了+[MFMessageComposeViewController canSendText]

MFMessageComposeViewController Class Reference

之前呈现消息组成视图,调用canSendText类方法,以确保用户的设备被适当地配置。如果canSendText方法返回NO,则不要尝试呈现消息组合视图。如果短信传送不可用,您可以通知用户或者简单地禁用应用程序中的SMS功能。

从iOS 5开始,您可以通过MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification通知来注册,通知发送文本消息的可用性发生变化。

原因,可能会返回nil

  • 设备未运行iOS 4
  • 设备是一部iPod touch/iPad上没有的iMessage功能。
  • 没有SIM卡? (该视图现在显示在iOS 6中;应用程序未收到消息发送失败通知。)
  • “设备”实际上是模拟器。 (也许这部作品在iOS 6中得。)

同样,[[MFMailComposeViewController alloc] init]回报nil时没有邮件帐户启用(可以快速通过禁用设置帐户测试此),但也显示出“没有配置邮件账户”警惕你。 MFMessageComposeViewController不会这样做。

+0

非常好,谢谢。我可能应该更清楚地认识到,使用模拟器出现问题显然没有SMS功能。 – Lee 2010-08-17 14:52:06

+0

如果没有SIM卡,则应用程序退出。为什么?它只是给出了一个弹出消息“不存在SIM卡”。它发送一个SMS发送结果给委托方法。然后退出。有没有办法阻止它退出? – karim 2010-10-08 09:26:35

+0

你检查了控制台的消息吗?它是否会崩溃或退出“正常”?有没有崩溃日志?附加调试器有帮助吗? – 2010-10-13 20:17:40