使用未声明的类型' MFMessageComposeViewControllerDelegate swift'

时间:2016-06-27 08:18:19

标签: ios swift sms

我正在尝试使用MFMessageComposeViewController对象实现发送文本消息。但是当我试图声明MFMessageComposeViewControllerDelegate协议时。在ViewController.swift中,我收到以下错误:使用未声明的类型' MFMessageComposeViewControllerDelegate swift'

ViewController.swift:

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {

@IBAction func sendMessage(sender: AnyObject) {
    var messageVC = MFMessageComposeViewController()

    messageVC.body = "Enter a message";
    messageVC.recipients = ["Enter tel-nr"]
    messageVC.messageComposeDelegate = self;

    self.presentViewController(messageVC, animated: false, completion: nil)
}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

添加MessageUI框架

import UIKit
import MessageUI

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {

}

希望它有用

答案 1 :(得分:1)

您只需添加

即可
import MessageUI

位于swift文件的顶部。另外,最好添加检查

if MFMessageComposeViewController.canSendText()

发送邮件之前,不要忘记在委托方法中删除邮件控制器

func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
    dismissViewControllerAnimated(true, completion: nil)
}

答案 2 :(得分:1)

您还需要委托来实施

func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
        switch result.rawValue {
        case MessageComposeResultCancelled.rawValue:
            print("message cancelled")

        case MessageComposeResultFailed.rawValue:
            print("message failed")

        case MessageComposeResultSent.rawValue:
            print("message sent")

        default:
            break
        }

        controller.dismissViewControllerAnimated(true, completion: nil)
    }

认为它有用!