我希望我的应用发送短信,而我目前的代码无效,以下是以下步骤:
import MessageUI
添加课程:
MFMessageComposeViewControllerDelegate
然后我用了一个按钮:
@IBAction func Messages(_ sender: UIButton) {
if MFMessageComposeViewController.canSendText() == true{
let recipients:[String] = ["1500"]
let messageController = MFMessageComposeViewController()
messageController.messageComposeDelegate = self // implement delegate if you want
messageController.recipients = recipients
messageController.body = "Your_text"
self.present(messageController, animated: true, completion: nil)
func messageComposeViewController(controller: MFMessageComposeViewController,
didFinishWithResult result: MessageComposeResult) {
// Dismiss the message compose view controller.
controller.dismiss(animated: true, completion: nil)}
}}
并使用函数:
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {}
所以我错过了什么?谢谢。
答案 0 :(得分:5)
@IBAction func Messages(_ sender: UIButton) {
if MFMessageComposeViewController.canSendText() == true {
let recipients:[String] = ["1500"]
let messageController = MFMessageComposeViewController()
messageController.messageComposeDelegate = self
messageController.recipients = recipients
messageController.body = "Your_text"
self.present(messageController, animated: true, completion: nil)
} else {
//handle text messaging not available
}
}
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: true, completion: nil)
}