简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM