Two ways iOS calls the system to send text messages

  • 2020-06-15 10:20:40
  • OfStack

1. Use the system to send text messages outside the program

This method is actually quite simple and calls openURL directly:


NSURL *url = [NSURL URLWithString:@"sms://15888888888"];
[[UIApplication sharedApplication]openURL:url];

2. Call the system to send SMS within the program

One benefit of this approach is that users can return to App after sending a text.

First import MessageUI.framework and introduce the header file:


#import <MessageUI/MessageUI.h>

Then follow the proxy MFMessageComposeViewControllerDelegate And implement the proxy method.


#pragma mark -  Proxy method 
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
  [self dismissViewControllerAnimated:YES completion:nil];
  switch (result) {
    case MessageComposeResultSent:
      // Successful transmission of information 
       
      break;
    case MessageComposeResultFailed:
      // Transmission failure 
       
      break;
    case MessageComposeResultCancelled:
      // The message was canceled by the user 
       
      break;
    default:
      break;
  }
}

Send short message method implementation


#pragma mark -  Sending SMS messages 
-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
  if( [MFMessageComposeViewController canSendText] )
  {
    MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
    controller.recipients = phones;
    controller.navigationBar.tintColor = [UIColor redColor];
    controller.body = body;
    controller.messageComposeDelegate = self;
    [self presentViewController:controller animated:YES completion:nil];
    [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];// Modify the title of the SMS interface 
  }
  else
  {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Prompt information "
                            message:@" The device does not support SMS "
                            delegate:nil
                       cancelButtonTitle:@" determine "
                       otherButtonTitles:nil, nil];
    [alert show];
  }
}

Finally, call the method that sends the message


[self showMessageView:[NSArray arrayWithObjects:@"15888888888",@"12399999999", nil] title:@"test" body:@" This is a test with SMS, do not reply! "];

Above is the site to introduce the iOS call system to send SMS two ways, I hope to help you.


Related articles: