使用MFMessageComposeViewController发送SMS

时间:2016-12-27 10:59:27

标签: swift3

我希望我的应用发送短信,而我目前的代码无效,以下是以下步骤:

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) {}

所以我错过了什么?谢谢。

1 个答案:

答案 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)
}