i am trying to implement sending a text message using the MFMessageComposeViewController object. But when i am trying to declare the MFMessageComposeViewControllerDelegate protocol. In ViewController.swift i get the following error : use of undeclared type '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.
}
}
Can someone please help me?
Add the MessageUI
Framework,
import UIKit
import MessageUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
}
hope its helpful
All you need is to add
import MessageUI
at the top of your swift file. Also, you better to add check
if MFMessageComposeViewController.canSendText()
before send message, and don't forget to dismiss your message controller in delegate method
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
dismissViewControllerAnimated(true, completion: nil)
}
You also need delegate to implement
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)
}
Think it's helpful!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.