0

我正在尝试实现一个 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

4 回答 4

10

消息的意思pickernil,即 MFMessageComposeViewController 没有创建成功。

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

 if (![MFMessageComposeViewController canSendText]) {
    // show message box for user that SMS cannot be sent
 } else {
    MFMessageComposeViewController* picker = ...;
    ...
 } 
于 2011-01-10T17:44:11.280 回答
2

很可能您正在 iPhone 模拟器中对此进行测试,MFMessageComposeViewController 在模拟器上不起作用并返回 nil

于 2011-08-23T13:09:29.440 回答
1

想到三件事。

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

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

第三,你确定picker不是nil吗?

于 2011-01-10T17:53:04.243 回答
0

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

于 2012-02-06T17:59:24.330 回答