Szabolcs Toth Szabolcs Toth - 1 month ago 12
Swift Question

Sending SMS from UITableViewController in Swift

I'd like to send SMS from my UITableViewController but the SMS window does't dismiss after sending or cancel.

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)

}

}

Answer

You never set the 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)

  }

}

You should also confirm that the device is setup to send messages. Please read the documentation for the MFMessageComposeViewController class. It explains all of this and shows sample code as well.

Comments