【问题标题】:Sending SMS from UITableViewController in Swift在 Swift 中从 UITableViewController 发送短信
【发布时间】:2016-09-12 17:54:17
【问题描述】:

我想从我的 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)

  }

}

【问题讨论】:

    标签: ios swift uitableview sms


    【解决方案1】:

    你永远不会设置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 类的文档。它解释了所有这些并显示了示例代码。

    【讨论】:

    • 我关注了this 文档,但我似乎打错了一些东西。谢谢!
    【解决方案2】:

    另一种解决方案是在 didSelect 上调用内置的 sms (Swift 3): UIApplication.shared.open(NSURL(string:"sms: number w/o spaces") as! URL)

    基于thisswift 2 解决方案或thisobjective-c 解决方案(见后半部分)。

    【讨论】:

      猜你喜欢
      • 2022-06-28
      • 2014-12-08
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多