21

In my application, MFMailComposeViewController works fine but creating a new instance of MFMessageComposeViewController fails.

Here is the code for both:

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

Its seemingly obvious that everything is linking correctly because the email view controller works fine. Is there something I am missing maybe configuration wise?

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Lee
  • 456
  • 1
  • 4
  • 7
  • As the questioner commented on the accepted answer the MFMailComposeViewController results in `nil` is because he used the Simulator. – Bruno Bieri Aug 07 '19 at 05:43

1 Answers1

51

Have you checked +[MFMessageComposeViewController canSendText]?

From the MFMessageComposeViewController Class Reference,

Before presenting a message composition view, call the canSendText class method to ensure that the user’s device is appropriately configured. Do not attempt to present a message composition view if the canSendText method returns NO. If SMS delivery isn’t available, you can notify the user or simply disable the SMS features in your application.

Starting in iOS 5, you can register to be notified of changes to the availability of text message sending by way of the MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification notification.

Reasons it might be returning nil:

  • Device isn't running iOS 4.
  • Device is an iPod Touch/iPad without iMessage enabled.
  • No SIM card? (The view now shows in iOS 6; the app is not notified of the message send failure.)
  • "Device" is actually the simulator. (Perhaps this works in iOS 6 too.)

Similarly, [[MFMailComposeViewController alloc] init] returns nil when no mail accounts are enabled (you can quickly test this by disabling accounts in Settings), but also shows a "No mail accounts configured" alert for you. MFMessageComposeViewController does not do this.

tc.
  • 33,468
  • 5
  • 78
  • 96
  • Excellent, thanks. I probably should have been more clear in that the issue arises using the simulator, which obviously doesn't have SMS capability. – Lee Aug 17 '10 at 14:52
  • If there is no SIM card, the app exits. why? it just gives a pop up message "no sim card present". it event send a SMS sent result to the delegate method. then exits. is there any way to prevent it from exiting? – karim Oct 08 '10 at 09:26
  • Have you checked the console for messages? Does it crash or exit "normally"? Is there a crash log? Does attaching a debugger help? – tc. Oct 13 '10 at 20:17
  • `"Device" is actually the simulator.` yes please, can't stress to point this out enough. – gabuchan Aug 06 '20 at 16:10