我刚开始学习编码,我正在尝试创建一个简单的代码来在 IOS 上发送消息。我已确保导入 MessageUI 框架。当我在我的模拟器上运行它时,它失败并显示 THREAD 1 : SIGNAL 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
}
}
}
非常感谢!
最佳答案
您好,尝试在物理设备上使用您的代码,它应该可以工作。
顺便说一句,请在您的代码中添加检查设备是否支持发送消息:
@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)
}
这样你就可以避免例如在模拟器上崩溃。
关于ios - 发送消息的 Swift 代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40564693/