2016-05-16 26 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解決方案(請參見後面的部分)。