I am trying to implement a SMS application. In that when I tried to send my sms I got an exception at [self.navigationController presentModalViewController:picker animated:YES];. I am very new to this. Can you guys please help me?. My code is as follows.
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];
My Log message is as follows,
Terminating app due to uncaught exception 'NSI开发者_JAVA技巧nvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <UINavigationController: 0x5b2c120>.
Thanks in Advance, S.
The message means picker
is nil
, i.e. the MFMessageComposeViewController is not created successfully.
Make sure [MFMessageComposeViewController canSendText]
returns YES, i.e..
if (![MFMessageComposeViewController canSendText]) {
// show message box for user that SMS cannot be sent
} else {
MFMessageComposeViewController* picker = ...;
...
}
Most probably you are testing this in your iPhone simulator, MFMessageComposeViewController does not work on a simulator and returns nil
Three things come to mind.
First, have you declared your view controller class to be implement MFMailComposeViewControllerDelegate? And have you defined mailComposeController:didFinishWithResult:error: ?
Second, you could just have: [self presentModalViewController:picker animated:YES];
Third, are you sure that picker is non-nil?
The main reason that the modal view will throw a nil exception usually has to do with the device under test not having an email account configured in settings (hence the other comment about the modal view not working in the simulator). @KennyTM's answer is a great way to handle this. Just popup an alert dialog notifying the user.
精彩评论