Search code examples
ios5mfmessagecomposeviewcontroller

iPhone iOS 5 SDK in app SMS error


thanks in advance every time I call to create an in app SMS i get the following error...

Application tried to push a nil view controller on target .

This is the code I have that worked in OS 4 SDK...

MFMailComposeViewController *MailController;
MFMessageComposeViewController *SMScontroller;
NSError *error;
NSString *EmailMessage;
NSString *SubjectMessage;

-(IBAction)sendInAppSMS
{

    if([MFMessageComposeViewController canSendText])
    {
        SMScontroller = [[MFMessageComposeViewController alloc] init];
        SMScontroller.messageComposeDelegate = self;

        NSString *MessageString = @"Hello";

        SMScontroller.body = MessageString;

        SMScontroller.navigationBar.tintColor = [UIColor blackColor];

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];

        [self presentModalViewController:SMScontroller animated:YES];

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
    }
    else{
        // alertView to tell user to setup a mail account. 
        NSString *message = [[NSString alloc] initWithFormat:@"To use this feature, you need to have an iPhone with SMS abilities."];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SMS Account Not Detected" message:message delegate:nil cancelButtonTitle:@"Understood" otherButtonTitles:nil];
        [alert show];
    }

}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch (result) {
        case MessageComposeResultCancelled:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"I Like It" message:@"User cancelled sending the SMS"
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
            break;
        case MessageComposeResultFailed:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"I Like It" message:@"Error occured while sending the SMS"
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
            break;
        case MessageComposeResultSent:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"I Like It" message:@"SMS sent successfully..!"
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
            break;
        default:
            break;
    }

    [self dismissModalViewControllerAnimated:YES];
}

Solution

  • I believe if you are running this in the simulator you'll get the error since it doesn't have sms. I get the same error in simulator but if i connect to my phone it works fine.