Mail Send from iPad

This is the very simple application. In this application we will see how to mail send from the iPad.

This is the very simple application. In this application we will see how to mail send from the iPad.

Step 1: Create a View base application using template. Give the application name “MailComposer”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Expand classes and notice Interface Builder created the MailComposerViewController class for you. Expand Resources and notice the template generated a separate nib, MailComposerViewController.xib, for the “MailComposer”.

Step 4: We need to add MessageUI Framework. Select Frameworks folder ->Add -> Existing Framework -> Add MessageUI Framework.

Step 5: In the MailComposerViewController.h file , we have import MessageUI framework. Create an instance of UIButton class and add one buttonPressed method. So make the following changes in the file.

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

@interface MailComposerViewController : UIViewController
<MFMailComposeViewControllerDelegate>
{
        IBOutlet UIButton *button;
}

- (IBAction)buttonPressed;

Step 6: Double click MailComposerViewController.xib file and open it to the Interface Builder. First drag the Round Rect button from the library and place it to the view window. Connect File’s Owner icon to the View icon and select view. Drag File’s Owner icon to the round Rect button and select button and select the Round Rect button and bring up Connection Inspector next drag Touch Up Inside to the File’s Owner icon and select buttonPressed: action. Now save the MailComposerViewController.xib file, close it and go back to the Xcode.

Step 7: Open the MailComposerViewController.m file and make the following changes in the file.

- (void)viewDidLoad {
        if ([MFMailComposeViewController canSendMail])
                button.enabled = YES;
}

- (IBAction)buttonPressed {
        MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
        mailController.mailComposeDelegate = self;
        [mailController setSubject:@"Hello World"];
        [mailController setMessageBody:@"This is the MailSend Application…." isHTML:NO];
        [self presentModalViewController:mailController animated:YES];
        [mailController release];
}

- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
        [self becomeFirstResponder];
        [self dismissModalViewControllerAnimated:YES];
}

Step 8: Now Compile and run the application in the Simulator.

You can Download SourceCode from here MailComposer

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

3 Responses to “Mail Send from iPad”

  1. Dominik says:

    I imported this project, but I am getting an error running the application on the iPad device. It works perfectly fine on the iPad simulator.

    The line of code it is crashing:

    [self presentModalViewController:mailController animated:YES];

    with this error:

    Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Application tried to present a nil modal view controller on target

    Dose anyone know what could cause this error?

  2. Kacper86 says:

    @Dominik:
    I found out, that unless you have mail configured you got this crash. My solution:

    MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setToRecipients:recipients];
    [controller setSubject:@""];
    [controller setMessageBody:body isHTML:NO];
    if (controller != nil) {
    [self presentModalViewController:controller animated:YES];
    [controller release];
    }

    This “if” helped :) I hope there won’t be a memory leak.

  3. I very glad to find this site on bing, just what I was searching for : D also bookmarked .

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG