Search code examples
iosswiftclassmfmailcomposeviewcontrollermfmessagecomposeviewcontroller

Adding BOTH Mail and SMS/MMS delegates to class type


I have a simple app. It emails and SMS/MMS a screenshot.

Since I have two buttons that each execute two different functions - (1) screenshot then email; and (2) screenshot then SMS/MMS, I need to add these to my class in my code.

Currently ...

import UIKit
import MessageUI


class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

How can I add MFMessageComposeViewControllerDelegate to my above class statement?

I think I need to as I have functions that send Email and SMS/MMS.

Apologies for my language description, I'm very new :)


Solution

  • also check this link : check this link

    import UIKit
    import MessageUI
    
    class ViewController: UIViewController, MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate {
    
    @IBOutlet weak var phoneNumber: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func sendText(sender: UIButton) {
        if (MFMessageComposeViewController.canSendText()) {
            let controller = MFMessageComposeViewController()
            controller.body = "Message Body"
            controller.recipients = [phoneNumber.text]
            controller.messageComposeDelegate = self
            self.presentViewController(controller, animated: true, completion: nil)
        }
    }
    
    
       func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
        //... handle sms screen actions
         self.dismissViewControllerAnimated(true, completion: nil)
        }
    
    override func viewWillDisappear(animated: Bool) {
        self.navigationController?.navigationBarHidden = false
    }
    
     @IBAction func sendMail(sender: UIButton)
     {
        let mailClass:AnyClass?  =NSClassFromString("MFMailComposeViewController")
       if(mailClass != nil)
       {
        if((mailClass?.canSendMail()) != nil)
        {
          displayComposerSheet()
        }
      }
    
      func displayComposerSheet()
      {
       let picker: MFMailComposeViewController=MFMailComposeViewController()
       picker.mailComposeDelegate=self;
       picker .setSubject("Test")
       picker.setMessageBody("Mail Sharing !", isHTML: true)
       let data: NSData = UIImagePNGRepresentation(UIImage(named: "images.jpg")!)!
       picker.addAttachmentData(data, mimeType: "image/png", fileName: "images.png")
       self.presentViewController(picker, animated: true, completion: nil)
      }
    
    
      func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        switch result.rawValue {
        case MFMailComposeResultCancelled.rawValue:
            print("Mail cancelled")
        case MFMailComposeResultSaved.rawValue:
            print("Mail saved")
        case MFMailComposeResultSent.rawValue:
            print("Mail sent")
        case MFMailComposeResultFailed.rawValue:
            print("Mail sent failure: \(error!.localizedDescription)")
        default:
            break
        }
        controller.dismissViewControllerAnimated(true, completion: nil)
      }
    
    
     }