iPhone - MFMailcomposeviewcontroller example

iPhone - MFMailcomposeviewcontroller example

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

Thanks!!

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..

    -(IBAction)showPicker:(id)sender
    {   
        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];
            }
            else
            {
                [self launchMailAppOnDevice];
            }
        }
        else
        {
            [self launchMailAppOnDevice];
        }
    }

    -(void)displayComposerSheet 
    {
        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";
            break;
        case MFMailComposeResultSaved:
            message.text = @"Result: saved";
            break;
        case MFMailComposeResultSent:
            message.text = @"Result: sent";
            break;
        case MFMailComposeResultFailed:
            message.text = @"Result: failed";
            break;
        default:
            message.text = @"Result: not sent";
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
    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
Advertisements
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
MapView Example in iPhone
MapView Example in iPhone The example illustrate how to embed or display a MapView in iPhone SDK UIView based application. To embed a Map in your iphone..._TO_REPLACE_8 Download code of MapView Example in iPhone
Iphone hello world example
Iphone hello world example In this tutorial an iphone based hello world example is demonstrated. Also some relevant information under based is provided which... applications. So lets start building your iphone first hello world example. Before
UINavigationBar Color - iPhone example
UINavigationBar Color - iPhone example The example is discussing about how to change the color of UINavigationBar. In iPhone applications you can customize..... In the example, we have assigned the black color to the UINavigationBar
Iphone Current Time Example
Iphone Current Time Example After going through this iphone SDK example, you will be able to show the current time on UILable. In this example we have used a UILable to show the output(current time) of the application and an UIButton
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
iphone  hiii, how can we use table recursively in iphone
iphone text field example
iphone textfield with example   In this iphone example you are going to see...how to add the value of two different textfield and printing it in another textfield. This is a quite easy! What all we have to do is to take three
iphone
iphone  hello... i want to make first iphone application . How can i make it plz help me
iphone
iphone  How can connect with the other database in iphone application(like... MYsql or oracle) is this dirctly possible
Hello World iphone Example
Hello World iphone with Example In the given tutorial you will be creating a small iphone application called "hello world app" using interface builder, that will run in the iphone simulator. Before going into deep
iphone button example
 iphone button example In this tutorial we will tell you how to change the type of button in Interface Builder. There are six types of iPhone button, each has used for different purpose.  1. Custom  2. Rounded
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
UIScrollView Example iPhone
UIScrollView Example iPhone UIScrollView is the superclass of several... iphone application window. That means you can adjust the size of view according... as well... frame size is the actual size of iPhone application window, whereas
iPhone Font Size
iPhone Font Size In this example we are going to change the iphone font size and color. You can change the font size and font type using the Inspector that resides in Interface Builder->Tools. It has the number of font
NSString CapitalizedString - Iphone example
NSString CapitalizedString - Iphone example Converting a first character of string to upper case (case changes): In this example, we are going to show you how to change the first character of the given string into uppercase. Create
iPhone resignfirstresponder
iPhone resignfirstresponder  Hi, Provide me a good example of iPhone resignfirstresponder. I need a code to hide keyboard when use clicks outside the text box. ThanksADS_TO_REPLACE_1   Hi, You can use the following
iPhone Application Icon
iPhone Application Icon The example will illustrate how to add an icon on the iPhone application in Xcode. Before adding the application icon in iPhone make... an application icon in iPhone simulator.. add the "png" icon that you want
iPhone MapView Current Location
iPhone MapView Current Location This is a simple MKMapView example in iPhone for the beginners. In the example we'll discuss about how to display a map... as current location but on running the application in iPhone it will give you
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
iPhone Graphics
iPhone Graphics  Hi, How to create iPhone Graphics? I am learning to crate UI Design for iPhone and iPad. 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 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 UIViewContentModeBottomRightADS_TO_REPLACE_1 In the example
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 UILabel Color
iPhone UILabel Color In this example we'll discuss about the UILabel class... the appearance of text in your iPhone applications. Final application will look alike..ADS... multiple lines of static text on label in our applications. For example if you wanted
iPhone
iPhone       Apple iPhone 3G is a multi-touch smartphone that has successfully combined multimedia, email, business and internet into a single platform. The iPhone
Embedding HTML in iPhone App
Embedding HTML in iPhone App This example illustrate you how to embed the HTML in iPhone application. To embed the HTML file we required a UIWebView... to embed HTML into UIWebView iphone appADS_TO_REPLACE_1 - (void)viewDidLoad
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
NSZombieEnabled iPhone
NSZombieEnabled iPhone  NSZombieEnabled Trouble in setting How can i set the NSZombieEnabled in iPhone application to track unmanaged memory
iphone - EXC_BAD_ACCESS
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
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
iPhone Splash Screen
iPhone Splash Screen In this example, I'm going to show you how to create splash screen in iphone apps. The motto behind showing splash screen is to show... in iphone apps, just create a "Default.png" image and put that image
Iphone Developmnt
Iphone Developmnt  i am making an application in ios, for ipad or iphone, what i want to know is that can i provide an option of changing the theme , or can i separate my code from the theme... so that when giving my product
Iphone Show Current Date & Time
Iphone Show Current Date & Time In the previous examples, we have printed current date and time separately in iphone simulator. But in this example we are going to show you how to print both current date and current time in iphone
Current Date iPhone SDK
Current Date iPhone SDK In this example, you'll find out how to get current date & day in iPhone SDK. We'll also write a method to print the day and date...! If not lets first find out what is NSdateFormatter in iPhone SDK programming
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
libxml2.2.dylib iphone
libxml2.2.dylib iphone  Hi, I added the libxml2.2.dylib in iPhone application, still its not able to compile the XML processing code. What to do? ThanksADS_TO_REPLACE_1   Hello, You must be getting the linking errors
iPhone String Match
iPhone String Match In this "String match" example we... that are used to search, compare and sort the strings in cocoa framework. For example... and comparing characters or substrings within the given strings. Let's take an example
example
example  example on Struts framework
example
example  example on Struts framework
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...; Hi, It is very easy to create a quiz application for iphone. The below link

Ads