iOS开发笔记-105:Swift5 - 调用系统发短信

import MessageUI

let alertController = UIAlertController(title: "发短信", message: "是否给\(cell_phonesS)发送短信?", preferredStyle: .alert)
        let cancleAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let sendAction = UIAlertAction(title: "确定", style: .default) { (alertController) in
            //判断设备是否能发短信(真机还是模拟器)
            if MFMessageComposeViewController.canSendText() {
                let controller = MFMessageComposeViewController()
                //短信的内容,可以不设置
//                controller.body = model.smsContent
                //联系人列表
                controller.recipients = [cell_phonesS]
                //设置代理
                controller.messageComposeDelegate = self
                self.present(controller, animated: true, completion: nil)
            } else {
                JJHUDManage.show("本设备不能发短信")
            }
        }
        alertController.addAction(cancleAction)
        alertController.addAction(sendAction)
        
        self.present(alertController, animated: true, completion: nil)

// MARK: - 短信
extension CardDetailVC: MFMessageComposeViewControllerDelegate {
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        controller.dismiss(animated: true, completion: nil)
        //判断短信的状态
        switch result{
        case .sent:
            JJHUDManage.show("短信已发送")
        case .cancelled:
            JJHUDManage.show("短信取消发送")
        case .failed:
            JJHUDManage.show("短信发送失败")
        default:
            JJHUDManage.show("短信已发送")
            break
        }
    }
    
    
}

推荐阅读更多精彩内容