- 首页 > it技术 > >
Swift|Swift 发送短信
//MARK: - 发送短信
func sendMessage(model: WebSendMessageModel) {
//创建一个弹出框提示用户
let alertController = UIAlertController(title: "发短信", message: "是否给\(model.phoneNum ?? String())发送短信?", 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 = [model.phoneNum] as? [String]
//设置代理
controller.messageComposeDelegate = self
self.present(controller, animated: true, completion: nil)
} else {
print("本设备不能发短信")
}
}
alertController.addAction(cancleAction)
alertController.addAction(sendAction)self.present(alertController, animated: true, completion: nil)
}//MFMessageComposeViewControllerDelegate
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {controller.dismiss(animated: true, completion: nil)
//判断短信的状态
switch result{case .sent:
print("短信已发送")
case .cancelled:
print("短信取消发送")
case .failed:
print("短信发送失败")
default:
print("短信已发送")
break
}
}
推荐阅读