Mail Send from 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

Leave a Reply

Your email address will not be published.