将文本从 UITextField 发送到 swift 中的短信

send text from UITextField to sms in swift

我有一个 UITextField,用户可以在其中输入一些文本。当我点击发送按钮时,我希望该文本作为短信发送。我写的代码给我带来了问题。这是我目前所拥有的。

import UIKit
import MessageUI

class ViewController: UIViewController,  MFMessageComposeViewControllerDelegate {



@IBOutlet weak var message: UITextField!


override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}


func canSendText() -> Bool {
    return MFMessageComposeViewController.canSendText()
}


func configuredMessageComposeViewController() -> MFMessageComposeViewController {

    let messageComposeVC = MFMessageComposeViewController()

    messageComposeVC.messageComposeDelegate = self
    messageComposeVC.body = message.text // This line doesn't work.
    return messageComposeVC
}


func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
    controller.dismissViewControllerAnimated(true, completion: nil)
}



@IBAction func sendtext(sender: AnyObject) {

    let messageComposer = ViewController()

        // Make sure the device can send text messages
        if (self.canSendText()) {
            // Obtain a configured MFMessageComposeViewController
            let messageComposeVC = messageComposer.configuredMessageComposeViewController()


            var vc = self.view?.window?.rootViewController

            vc?.presentViewController(messageComposeVC, animated: true, completion: nil)



        } else {
            // Let the user know if his/her device isn't able to send text messages
            let errorAlert = UIAlertView(title: "Cannot Send Text Message", message: "Your device is not able to send text messages.", delegate: self, cancelButtonTitle: "OK")
            errorAlert.show()
        }




}


}

有没有办法复制 UITextField 的所有内容并粘贴到 messageComposeVC.body?任何帮助表示赞赏。谢谢!

尝试更改行

let messageComposeVC = messageComposer.configuredMessageComposeViewController()

let messageComposeVC = self.configuredMessageComposeViewController()

messageComposer 是您的 ViewController 的新实例,它没有消息集。您正在

行中创建 ViewController 的新实例
var messageComposer = ViewController()

这有一个 message textField 的新实例,其中不包含您输入的文本。