在iPhone sdk中问题MFMessageComposerViewController

时间:2011-01-10 17:37:21

标签: iphone mfmailcomposeviewcontroller

我正在尝试实施SMS应用程序。当我试图发送我的短信时,我在[self.navigationController presentModalViewController:picker animated:YES];中得到了一个异常。我对此很新。你能帮帮我吗?我的代码如下。

MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.delegate = self;

picker.recipients = [NSArray arrayWithObject:@"123456789"]; // your recipient number or self for testing
picker.body = @"test from OS4";

[self.navigationController presentModalViewController:picker animated:YES];
[picker release];

我的日志消息如下,

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <UINavigationController: 0x5b2c120>.

先谢谢, S上。

4 个答案:

答案 0 :(得分:10)

消息表示pickernil,即未成功创建MFMessageComposeViewController。

确保[MFMessageComposeViewController canSendText]返回YES,即..

 if (![MFMessageComposeViewController canSendText]) {
    // show message box for user that SMS cannot be sent
 } else {
    MFMessageComposeViewController* picker = ...;
    ...
 } 

答案 1 :(得分:2)

很可能你是在iPhone模拟器中测试它,MFMessageComposeViewController不能在模拟器上运行并返回nil

答案 2 :(得分:1)

有三件事情浮现在脑海中。

首先,您是否已将视图控制器类声明为实现MFMailComposeViewControllerDelegate?你定义了mailComposeController:didFinishWithResult:error:?

其次,您可以:[self presentModalViewController:picker animated:YES];

第三,你确定选择器是非零的吗?

答案 3 :(得分:0)

模态视图抛出零异常的主要原因通常与被测设备没有在设置中配置电子邮件帐户有关(因此关于模态视图的其他评论在模拟器中不起作用)。 @ KennyTM的答案是解决这个问题的好方法。只需弹出一个警告对话框通知用户。