The Message UI framework provides specialized view controllers for presenting standard composition interfaces for email and SMS (Short Messaging Service) text messages. Use these interfaces to add message delivery capabilities without requiring the user to leave your app.

To display a composition interface, present the corresponding view controller modally from your app. Once presented, the user has the option to customize the contents before sending or canceling the message. Your custom delegate object then handles the dismissal of the view controller based on the user’s action.Apple Documentation.

Implementing this is very easy.

Steps:

  1. create a Xcode Project(Lets Say EmailWithAttachment) and take one Button.

Screen Shot 2016-07-20 at 12.37.34 AM

2. Now make Proper Action for the Button.

3. Add MessageUI framework in Your Project

Screen Shot 2016-07-20 at 12.18.06 AM.png

4.  Add one image in your project which we will attach with the Email.

5. Now go to ViewController.m and write the following code.

#import “ViewController.h”

#import <MessageUI/MessageUI.h>

@interface ViewController ()<MFMailComposeViewControllerDelegate>

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

– (IBAction)sendEmailButtonAction:(id)sender {

    NSArray *toRecipents = [NSArray arrayWithObject:@”abc@gmail.com”];

    MFMailComposeViewController *mfmailComposer = [[MFMailComposeViewController alloc] init];

    mfmailComposer.mailComposeDelegate = self;

    [mfmailComposer setSubject:@”Image transfer Mail”];

    [mfmailComposer setMessageBody:@”Check the tutorial iostpoint.wordpress.com” isHTML:NO];

    [mfmailComposer setToRecipients:toRecipents];

  // Get the resource path and read the file using NSData

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@”kidsBgView” ofType:@”png”];

    NSData *fileData = [NSData dataWithContentsOfFile:filePath];

    // Add attachment

    [mfmailComposer addAttachmentData:fileData mimeType:@”image/png” fileName:@”kidsBgView.png”];

    // Present mail view controller on screen

    [self presentViewController:mfmailComposer animated:YES completion:NULL];

 

}

#pragma mark – MFMailDelgateMethods

– (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error

{

    switch (result)

    {

        case MFMailComposeResultCancelled:

            NSLog(@”Mail was cancelled”);

            break;

        case MFMailComposeResultSaved:

            NSLog(@”You Saved mail”);

            break;

        case MFMailComposeResultSent:

            NSLog(@”Mail is successfully sent”);

            break;

        case MFMailComposeResultFailed:

            NSLog(@”Mail sent failure: %@”, [error localizedDescription]);

            break;

        default:

            break;

    }

 

    [self dismissViewControllerAnimated:YES completion:NULL];

}

@end

6. Run the code and see the desired output. Make sure that you are running it in a device in which mail in configured.

You can download the whole Xcode Project from here.

If you like this post, Please give your valuable Feedback.

Enjoy Coding 🙂

Advertisements