4

I am using the following code within my application to create and populate a SMS for the user to send. This code is called by pressing a UIButton.

MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"Hello from Mugunth";
        controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];
        controller.messageComposeDelegate = self;
        [self presentModalViewController:controller animated:YES];
    }

On the initial loading of the view, I would like to test the device capabilities, and hide the button if necessary (for example on an iPod touch).

Are there any code examples on how to do this? Please note, I am only targeting iOS 4.0 and greater, I am aware the above code will not work on devices using earlier versions of iOS.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mick Walker
  • 3,862
  • 6
  • 47
  • 72
  • Does [MFMessageComposeViewController canSendText] take care of the functionaility I require? I do not have an iPod touch device to test upon. – Mick Walker Mar 05 '11 at 22:19

3 Answers3

4

[MFMessageComposeViewController canSendText] will determine whether the device you are on can send text messages. I've known it to react correctly on an iPhone with SIM, an iPod Touch and the iOS Simulator. I've not tested it on e.g. an iPhone without SIM or an iPad.

Tommy
  • 99,986
  • 12
  • 185
  • 204
2

You can use the below code to detect whether the device can send SMS or not:

Preferred:

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"sms:"]]) {

}

or

[MFMessageComposeViewController canSendText]
FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
0

Make sure you call [MFMessageComposeViewController canSendText] prior to allocating an instance of MFMessageComposeViewController. Newer versions of iOS will throw an alert informing the user that they can't send messages as soon as you instantiate MFMessageComposeViewController.

Brian Antonelli
  • 736
  • 9
  • 16