Sunday, December 28, 2014

MFMailComposeViewController - Write an email with an image attachment

Updated with Xcode 7.1/Swift 2.1 (November 11, 2015):

Edit ViewController.swift as:

import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
    
    var myUIImage : UIImage!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let button = UIButton(frame: CGRectMake(100, 100, 100, 20))
        button.setTitle("Press", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "emailButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(button)
    }
    
    func emailButtonPressed(sender: UIButton) {
        
        let myController = MFMailComposeViewController()
        myController.mailComposeDelegate = self
        myController.setSubject("Hello 123")
        myController.setMessageBody("Hello World!", isHTML: false)
        myController.setToRecipients(["mymail@mymail.com"])
        
        let imageData = UIImagePNGRepresentation(UIImage(named: "image.png")!)
        myController.addAttachmentData(imageData!, mimeType: "image/png", fileName: "image")
        
        self.presentViewController(myController, animated: true, completion: nil)
    }

    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        if result.rawValue == MFMailComposeResultSent.rawValue {
            self.dismissViewControllerAnimated(false, completion: {
                let alertController = UIAlertController(title: "", message: "Mail Sent!", preferredStyle: UIAlertControllerStyle.Alert)
                alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            
                self.presentViewController(alertController, animated:true, completion:nil)
            })
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Original post (December 28, 2014):

1. Create a UIButton and a UIImageView

2. Include an image file image.png (or a photo) in the project

3. Write the code as below:



import UIKit

import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

    @IBOutlet weak var myImage: UIImageView!
    
    var myUIImage : UIImage!
    
    @IBAction func emailButtonPressed(sender: UIButton) {
        
        var myController = MFMailComposeViewController()
        myController.mailComposeDelegate = self
        myController.setSubject("Hello 123")
        myController.setMessageBody("Hello World!", isHTML: false)
        myController.setToRecipients(["mymail@mymail.com"])
        
        var imageData = UIImagePNGRepresentation(myUIImage)
        myController.addAttachmentData(imageData, mimeType: "image/png", fileName: "image")
        
        self.presentViewController(myController, animated: true, completion: nil)
    }
    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
        if result.value == MFMailComposeResultSent.value {
            let alertView = UIAlertView()
            alertView.message = "Mail Sent!"
            alertView.addButtonWithTitle("OK")
            
            alertView.show()
        }
        self.dismissViewControllerAnimated(false, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        myUIImage = UIImage(named: "image.png")
        
        self.myImage.image = myUIImage


2 comments: