Question

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.

Was it helpful?

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

OTHER TIPS

There's a method on MFMEssageComposeViewController:

if ([MFMessageComposeViewController canSendText]) {

}

else {
    NSLog(@"Cannot send text");
}
    NSString *deviceType = [UIDevice currentDevice].model;
    if ([deviceType hasPrefix:@"iPod"])
    {
        //It's iPod;
        //Disable button
    }

First detect device by using following way and if device does not support messenger than show alert.

Form here you can get more idea about different device detection : Determine device (iPhone, iPod Touch) with iPhone SDK

- (IBAction)Message:(id)sender
{
   NSString *deviceType = [UIDevice currentDevice].model;

    if([deviceType isEqualToString:@"iPod Touch 5G"])  {

         // here your alert view to show msg 


    } else {

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

You can use the UIDevice class to check the device type

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType hasPrefix:@"iPod"])
    // it's an iPod

or you can use [MFMessageComposeViewController canSendText] to check the message can be send from the device or not

 -(IBAction)btnByEmailPressed:(id)sender
 {
     if ([MFMailComposeViewController canSendMail] == NO)
 {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"This device is not able to send mail or Email account is not configured." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
       [alert show];
       return;
   }
   else
   {

       MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
       controller.mailComposeDelegate = self;
       [controller setTitle:@"Invitation"];
       [controller setSubject:@"My Subject"];
       [controller setMessageBody:@"Your Text" isHTML:YES];
       [self presentViewController:controller animated:YES completion:nil];
   }
}

 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
[self becomeFirstResponder];
NSString *strMailResult;
switch (result)
{
    case MFMailComposeResultCancelled:
        strMailResult = NSLocalizedString(@"E-Mail Cancelled",@"");
        break;
    case MFMailComposeResultSaved:
        strMailResult = NSLocalizedString(@"E-Mail Saved",@"");
        break;
    case MFMailComposeResultSent:
        strMailResult = NSLocalizedString(@"E-Mail Sent",@"");
        break;
    case MFMailComposeResultFailed:
        strMailResult = NSLocalizedString(@"E-Mail Failed",@"");
        break;
    default:
        strMailResult = NSLocalizedString(@"E-Mail Not Sent",@"");
        break;
}
   UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"STEP-BY-STEP-STORY",@"") message:strMailResult delegate:self  cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
   [alertView show];
   [self dismissViewControllerAnimated:YES completion:nil];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top