iPhone - MFMailcomposeviewcontroller example

iPhone - MFMailcomposeviewcontroller example

Hi! I'm looking for a mfmailcomposeviewcontroller example for both iPhone and iPad. Any suggestion??


View Answers

April 5, 2011 at 4:29 PM

To configure a mail in your iPhone / iPad application, import the given class in your header file. And add a new framework called "MessageUI.framework" to your frameworks folder.

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

April 5, 2011 at 4:42 PM

We also need to create a label to show the message and button action to perform actions as showPicker, displayComposerSheet, launchMailAppOnDevice as given below..

        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
        if (mailClass != nil)
            // We must always check whether the current device is configured for sending emails
            if ([mailClass canSendMail])
                [self displayComposerSheet];
                [self launchMailAppOnDevice];
            [self launchMailAppOnDevice];

        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;

        [picker setSubject:@"Hello from California!"];

        // Set up recipients
        NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
        NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 
        NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 

        [picker setToRecipients:toRecipients];
        [picker setCcRecipients:ccRecipients];  
        [picker setBccRecipients:bccRecipients];

        // Attach an image to the email
        NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];

        // Fill out the email body text
        NSString *emailBody = @"It is raining in sunny California!";
        [picker setMessageBody:emailBody isHTML:NO];

        [self presentModalViewController:picker animated:YES];
        [picker release];
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
    message.hidden = NO;
    // Notifies users about errors associated with the interface
    switch (result)
        case MFMailComposeResultCancelled:
            message.text = @"Result: canceled";
        case MFMailComposeResultSaved:
            message.text = @"Result: saved";
        case MFMailComposeResultSent:
            message.text = @"Result: sent";
        case MFMailComposeResultFailed:
            message.text = @"Result: failed";
            message.text = @"Result: not sent";
    [self dismissModalViewControllerAnimated:YES];

// Launches the Mail application on the device.
    NSString *recipients = @"mailto:[email protected][email protected],[email protected]&subject=Hello from California!";
    NSString *body = @"&body=It is raining in sunny California!";

    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

You can find a running example in Apple website for developers too :-)


Related Tutorials/Questions & Answers:
info button iphone example
info button iphone example  I'm looking for a simple info button example in iPhone XCode. Thanks
uialertview example iphone
uialertview example iphone  Hi, How to write uialertview example iphone code? Thanks   Hi, Please check UIAlertView Example. Thanks
iPhone Radio Button Example
iPhone Radio Button Example  Can you please suggest me how to create Radio button in my iPhone application. Actually, in my application i have two images on UIButton 1) selected and 2) unselected ..what i want is, when a user
How to write Example code for comparing Strings in iPhone SDK?
How to write Example code for comparing Strings in iPhone SDK?  I have an object of NSString class and I want to compare it with another NSString... at iPhone SDK Comparing Strings. Thanks
iphone  How can connect with the other database in iphone application(like... MYsql or oracle) is this dirctly possible
iphone avfoundation
iphone avfoundation  What is iphone avfoundation and why it is required? Can anyone show how to implement the AVFoundation in iPhone application with example or tutorial? Thanks
GLImageProcessing iphone sample
GLImageProcessing iphone sample  Hi, From where I can download the GLImageProcessing example for iPhone? Thanks   Hi, The GLImageProcessing sample for iPhone shows how to use OpenGL ES libraries to bright, sharp
iPhone SDK UIAlertView
iPhone SDK UIAlertView  Hi, How can i use the iPhone UIAlertView to create a message box. Can any help or provide example or online reference. Thanks
Iphone Current Date Application
Iphone Current Date Application  Hi, I am a beginner in the mobile web application segment. How can i develop Iphone Current Date Application. I need the online help guide or example about iphone current date format. Thanks
Iphone Table View Application
Iphone Table View Application  Hi, How to use Table View Application in Iphone? What parameters i have to follow to develop the Table view Apps with iphone? Please guide me or suggest any online example for reference. Thanks
How to Make iPhone Window Application
How to Make iPhone Window Application  Hi, For Making iphone window application, what parameters i have to follow or provide example related to iphone window application. Thanks
iPhone Graphics
iPhone Graphics  Hi, How to create iPhone Graphics? I am learning to crate UI Design for iPhone and iPad. Thanks
Iphone call
Iphone call  Do you have other method to make a call on Iphone,without openUrl
iPhone Single Tap and Double Tap Example
iPhone Single Tap and Double Tap Example The example discuss about single tapping / double tapping on UIView and it also explains how to represent the UIIMage... UIViewContentModeBottomLeft UIViewContentModeBottomRight In the example, to detect the end
Iphone Tutorial
Iphone Tutorial  Hi, I am a new in this mobile applications segment. From where i will get the information about iphone tutorial or Iphone Development Tutorials. Please suggest the resouces......... Thanks
share data iphone sdk
share data iphone sdk  How to share data in iPhone application? Actually i want the sharing feature in my iPhone application. Thanks!   You can use the emailing feature in your iPhone application to the share data
iPhone       Apple iPhone 3G is a multi-touch smartphone that has successfully combined multimedia, email, business and internet into a single platform. The iPhone
How to Create Segment Application with iPhone
How to Create Segment Application with iPhone  Hi, i am fresher in iphone application. How can i will create segment application in iphone. So, kindly help me or provide online example reference. Thanks
iphone - EXC_BAD_ACCESS  What cause this "iphone - EXCBADACCESS.... For example, you have created the NSArray or NSMutable array , initialization.... That could be the cause of iphone - EXCBADACCESS error
multiple language support iPhone
an application in iPhone that can support multiple languages.   Yes, iPhone/iPad applications support the multi language applications. To create your iPhone... the project location and create two project files for example en.lproj
NSZombieEnabled iPhone
NSZombieEnabled iPhone  NSZombieEnabled Trouble in setting How can i set the NSZombieEnabled in iPhone application to track unmanaged memory
Page curl effect iPhone SDK
Page curl effect iPhone SDK  How to get the page curl effect in my iPhone application? Please suggest or give an example to get the page curl effect in iPhone SDK application. Thanks.   In objective c there is a method
How to Create Quiz Application using iPhone
iphone application Programming Language. my question is that how to create a quiz application using iPhone. Please provide online reference to study and example. Thanks,   Hi, It is very easy to create a quiz application for iphone
iphone nsurlrequest authentication
iphone nsurlrequest authentication   How to authenticate a form in iPhone Application
How to Convert NSStringe into Uppercasestring iPhone
check the iPhone NSString uppercaseString() example. Thanks   Hi, For how to convert nsstring into uppercasestring in iphone Application. Please... how to convert nsstring into uppercasestring in iphone Application. Thanks
How to Changing Toolbar Color using iPhone Application ?
the online reference or example of how to change toolbar color in iPhone Apps...How to Changing Toolbar Color using iPhone Application ?  Hi, i have developing a small application to changing the toolbar color in iphone. But i
example  example on Struts framework
example  example on Struts framework
what is json in iphone sdk
what is json in iphone sdk  What is json in iphone sdk? Is this a library and when i need to include JSon in my iPhone SDK App