Swift 发送消息不工作的代码

标签: Xcode ios Swift
发布时间: 2017/1/22 19:50:34
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我学到的代码和我试图创建简单的代码来在 IOS 上发送一条消息。我已确认要导入的 MessageUI 框架。当我在我的模拟器上运行它时,它将失败,并显示线程 1︰ 信号 SIGABRT。道歉的是,如果它是只是一个小的错误,,因为我常做这样的错误,不想浪费任何的人时间大汗。这里是我的代码︰

import UIKit
import MessageUI


class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {



@IBOutlet weak var messageLabel: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
 @IBAction func SendSMS(sender: AnyObject) {
    let messageVC = MFMessageComposeViewController()

    messageVC.recipients = ["5146276051"]
    messageVC.body = messageLabel.text
    messageVC.messageComposeDelegate =  self
    self.presentViewController(messageVC, animated: true, completion: nil)


}
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {

    switch (result.rawValue) {

    case MessageComposeResultCancelled.rawValue:
        print("Message was cancelled")
        self.dismissViewControllerAnimated(true, completion: nil)

    case MessageComposeResultFailed.rawValue:
        print("Message has failed")
        self.dismissViewControllerAnimated(true, completion: nil)

    case MessageComposeResultSent.rawValue:
        print("Message was sent")
        self.dismissViewControllerAnimated(true, completion: nil)

    default:
        break
    }
}

}

谢谢!

解决方法 1:

嗨尝试在物理设备上使用您的代码,它应该工作。

顺便说一句你的代码中请添加检查是否设备支持发送消息︰

        @IBAction func SendSMS(sender: AnyObject) {
            //Checking whether device support message sending
            if (MFMessageComposeViewController.canSendText() == false) { return }

            let messageVC = MFMessageComposeViewController()

            messageVC.recipients = ["5146276051"]
            messageVC.body = messageLabel.text
            messageVC.messageComposeDelegate =  self
            self.presentViewController(messageVC, animated: true, completion: nil)
        }

在这你会避免的方式例如崩溃在模拟器上。

官方微信
官方QQ群
31647020