Search code examples
iosiphoneipodmfmessagecomposeviewcontroller

ios MFMessageComposeViewController ipod crash


Hi I am using MFMessageComposeViewController for messaging in an iPhone app. As this is an iPhone app it also supports iPod. And when clicking on the message button the app crashes as messaging is not available on iPod. So is there a way to check whether the device is an iPod so that i can hide the message button so that the user may not click on message in iPod and crash.

This is the code I have used for messaging.

- (IBAction)Message:(id)sender
{
MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
messaging.messageComposeDelegate=self;
[messaging setBody:@"Will of the People""\n""http://bit.ly/1gZhZye"];

[self presentViewController:messaging animated:YES completion:nil];
}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:^{UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Done" message:nil delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
    [alert show];
}];
}

And this seems to be working fine in iPhone. I need a way to disable this button when the user is using iPod.


Solution

  • You can use the canSendText class method for doing this:

    - (IBAction)Message:(id)sender
    {
       if ([MFMessageComposeViewController  canSendText])
       {
          MFMessageComposeViewController *messaging=[[MFMessageComposeViewController alloc]init];
          messaging.messageComposeDelegate=self;
          [messaging setBody:@"Will of the People""\n""http://bit.ly/1gZhZye"];
          [self presentViewController:messaging animated:YES completion:nil];
       }
    }
    

    Reference :

    canSendText

    Returns a Boolean value indicating whether the current device is capable of sending text messages. + (BOOL)canSendText

    Return Value

    YES if the device can send text messages or NO if it cannot. Discussion

    Always call this method before attempting to present the message compose view controller. A device may be unable to send messages if it does not support messaging or if it is not currently configured to send messages. This method applies only to the ability to send text messages via iMessage, SMS, and MMS.

    To be notified of changes in the availability of sending text messages, register as an observer of the MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification notification. Availability

    Available in iOS 4.0 and later.
    

    Declared In MFMessageComposeViewController.h