–>MsgViewController<–
class MsgViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.orange
let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
self.view.addSubview(btn)
btn.setTitle("Send Msg", for: .normal)
btn.backgroundColor = UIColor.blue
btn.addTarget(self, action: #selector(self.sendMsg), for: .touchDown)
//constraints
btn.translatesAutoresizingMaskIntoConstraints = false
//method 1
/*
NSLayoutConstraint.activate([
btn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
btn.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
btn.widthAnchor.constraint(equalToConstant: btn.frame.width),
btn.heightAnchor.constraint(equalToConstant: btn.frame.height)
])
*/
//method 2
btn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
btn.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
btn.widthAnchor.constraint(equalToConstant: btn.frame.width).isActive = true
btn.heightAnchor.constraint(equalToConstant: btn.frame.height).isActive = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func sendMsg(){
if MFMessageComposeViewController.canSendText() {
let c = MFMessageComposeViewController()
c.body = "短信内容,欢迎来到jobyme88.com"
c.recipients = ["15866668888"]
c.messageComposeDelegate = self
self.present(c, animated: true, completion: nil )
} else {
print("本设备不能发送短信")
}
}
}
extension MsgViewController: MFMessageComposeViewControllerDelegate{
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
switch result {
case .sent:
print("已发送")
case .cancelled:
print("用户取消")
case .failed :
print("发送失败")
}
}
}