情诗网 >情话短信 > 正文

iOS调用短信功能

来源:情诗网    2020-12-15    分类:情话短信

  1. 在程序中添加框架:MessageUI.framework
  2. 引入头文件:
#import<MessageUI/MessageUI.h>
  1. 实现如下代码:
 BOOL canSendSMS = [MFMessageComposeViewController canSendText];
   if(canSendSMS) {
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init]; // 创建短信视图控制器
        picker.messageComposeDelegate = self; // 设置代理
        picker.body = @"设置短信内容";
        picker.recipients = [NSArray arrayWithObjects:@"13811111111"]; // 设置电话,可同时设置多个电话实现群发
        [self presentModalViewController:picker animated:YES]; // 打开短信功能
   }
  1. 设置代理**<MFMessageComposeViewControllerDelegate> **//监控发送短信的状态
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{

    switch(result) {
        case MessageComposeResultCancelled:
            NSLog(@"Reseult:canceled"); //取消发送短信功能
            break;
        case MessageComposeResultSent:
            NSLog(@"Result:Sent"); //发送短信
            break;
        case MessageComposeResultFailed:
            NSLog(@"Result:Failed");  //发送失败
            break;
        default:
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

热门文章