Search code examples
iphoneobjective-cios5

Cancel button on MFMessageComposeViewController does not show up


In my iPhone application, I have just implemented in-app SMS functionality. SMS functionality is working fine. But after opening MFMessageComposeViewController, if user wants to cancel sending sms, they have no option. The only option left is to send an sms, then only return on previous view. There should be a cancel button on navigation bar just as it is in the email composer. Below is the line of code that I wrote to have in-app sms functionality:

-(void) smsComposer{
     MFMessageComposeViewController *_smsCompose = [[MFMessageComposeViewController alloc] init];
     if ([MFMessageComposeViewController canSendText]) {
         _smsCompose.body = @"SMS BODY";
         _smsCompose.messageComposeDelegate = self;
         [self presentModalViewController:_smsCompose animated:YES];
    }
}

Is there anything that I am missing?

Thanks in advance, PC


Solution

  • Try This....

    in .h file

    #import <MessageUI/MFMessageComposeViewController.h>
    

    and

    @interface TestViewController : UIViewController <MFMessageComposeViewControllerDelegate>
    

    And then Button Click method

    -(void)buttonPressed:(UIButton *)button
    {
    [self sendSMS:@"Body of SMS..." recipientList:[NSArray arrayWithObjects:@"+1-111-222-3333", @"111-333-4444", nil]];
    }
    

    MFMessageComposeViewController to create the SMS content and another method for handling the user interaction with the SMS dialog.

    -(void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
        {
          MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
        if([MFMessageComposeViewController canSendText])
        {
          controller.body = bodyOfMessage;    
          controller.recipients = recipients;
          controller.messageComposeDelegate = self;
          [self presentModalViewController:controller animated:YES];
        }    
     }
    

    And

     -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
        {
          [self dismissModalViewControllerAnimated:YES];
    
          if (result == MessageComposeResultCancelled)
            NSLog(@"Message cancelled")
          else if (result == MessageComposeResultSent)
            NSLog(@"Message sent")  
          else 
            NSLog(@"Message failed")  
        }
    

    And remember: You cannot send SMS messages from within the simulator. Test on Device.