2016-05-16 68 views
0

我想从我的UITableViewController发送短信,但发送或取消后短信窗口不会关闭。在Swift中从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) 

    } 

} 

回答

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课程的文档。它解释了所有这些,并显示示例代码。

+0

我跟着[this](https://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMessageComposeViewController_class/)文档,但我似乎错误的东西。谢谢! –

0

另一种解决方案是调用内置的SMS(斯威夫特3)didSelect: UIApplication.shared.open(NSURL(字符串: “短信:数W/O空间”)作为URL!)

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