从Swift中的UITableViewController发送SMS

时间:2016-05-16 20:10:15

标签: ios swift uitableview sms

我想从我的UITableViewController发送短信,但发送或取消后短信窗口不会被忽略。

import MessageUI

class TableViewController: UITableViewController, UISearchResultsUpdating {

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let msgVC = MFMessageComposeViewController()

    msgVC.recipients = ["555555"]

    self.presentViewController(msgVC, animated: true, completion: nil)
  }

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

    self.dismissViewControllerAnimated(true, completion: nil)

  }

}

2 个答案:

答案 0 :(得分:4)

您从未设置messageComposeDelegate

import MessageUI

class TableViewController: UITableViewController, UISearchResultsUpdating, MFMessageComposeViewControllerDelegate {

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let msgVC = MFMessageComposeViewController()
    msgVC.messageComposeDelegate = self

    msgVC.recipients = ["555555"]

    self.presentViewController(msgVC, animated: true, completion: nil)
  }

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

    self.dismissViewControllerAnimated(true, completion: nil)

  }

}

您还应确认设备已设置为发送消息。请阅读MFMessageComposeViewController课程的文档。它解释了所有这些并显示了示例代码。

答案 1 :(得分:0)

另一种解决方案是在didSelect上调用内置短信(Swift 3): UIApplication.shared.open(NSURL(字符串:“sms:number w / o spaces”)as!URL)

基于this swift 2解决方案或this objective-c解决方案(见后半部分)。