iOS开发跳转到发送短信界面并实现发短信

时间:2022-09-16 08:58:00


一、导入头文件

#import <MessageUI/MessageUI.h>

二、创建

@property (nonatomic,strong) MFMessageComposeViewController *picker;

三、初始化

  if ([MFMessageComposeViewControllercanSendText]) {

       self.picker = [[MFMessageComposeViewControlleralloc]init];

         self.picker.messageComposeDelegate = self;

        //推荐的电话号码

        NSString *phoneNum =@"13986097929";

         self.picker.recipients =@[phoneNum];

         self.picker.body = [NSStringstringWithFormat:@"测试数据%@",@"24355"];

         self.picker.navigationBar.tintColor = [UIColor redColor];

        [selfpresentViewController: self.pickeranimated:YEScompletion:nil];


    }else{

        

    }


四、代理方法处理

-(void)messageComposeViewController:(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result

{

    switch (result) {

        caseMessageComposeResultSent:

            NSLog(@"信息传送成功");

          [selfdismissViewControllerAnimated:YES completion:nil];

            break;

        caseMessageComposeResultFailed:

             NSLog(@"信息传送失败");

         [self dismissViewControllerAnimatedYES completion:nil];

            break;

        caseMessageComposeResultCancelled:

             NSLog(@"信息被用户取消传送");

            

           [selfdismissViewControllerAnimated:YES completion:nil];

            

            break;

        default:

            break;

    }

}