Mail Composer in iPad

Introduction

In this article I will create a Single View application with a button from Xib. When we click on the button, the message composer opens.

To understand it we use the following.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a new XCode Project by clicking on it.

Step 3

Now select Single View Application and click on Next.

Step 4

Now provide your Product Name and Company Identifier.

Step 5

Select the location where you want to save your project and click on Create.

Step 6

Now we write the code.

ViewController.h

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController < MFMailComposeViewControllerDelegate>
{
IBOutlet UIButton *button;
}
- (IBAction)buttonPressed;
@end

ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
if ([MFMailComposeViewController canSendMail])
button.enabled = YES;
}
- (IBAction)buttonPressed {

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

mailController.mailComposeDelegate = self;

[mailController setSubject:@"Hello Sachin"];

[mailController setMessageBody:@"This is the Email 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];

}
- (void)dealloc {

[button release];

[super dealloc];

}
@end

Step 7

Finally we click on the Run button to show the output.

Step 8

Output1 in iPad:

Output1-in-iPad.png

Output2 in iPad:

Output2-in-iPad.png

Output3 in iPad:

Output3-in-iPad.png

Up Next
    Ebook Download
    View all
    Learn
    View all