Thursday, 12 June 2014

iOS - Sending Email

Create By: Osama Sheikh
(S.E) And I,T Expert
_________________________________
Post By: 
Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

iOS - Sending Email

Introduction

We can send email using the Email application of iOS device.

Steps Involved

1. Create a simple View based application.
2. Select your project file, then select targets and then add MessageUI.framework.
3. Add a button in ViewController.xib and create an action for sending email.
4. Update ViewController.h as follows.
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>
{
    MFMailComposeViewController *mailComposer;
}

-(IBAction)sendMail:(id)sender;

@end
4. Update ViewController.m as follows.
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)sendMail:(id)sender{
    mailComposer = [[MFMailComposeViewController alloc]init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:@"Test mail"];
    [mailComposer setMessageBody:@"Testing message 
    for the test mail" isHTML:NO];
    [self presentModalViewController:mailComposer animated:YES];
}

#pragma mark - mail compose delegate
-(void)mailComposeController:(MFMailComposeViewController *)controller 
 didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
   if (result) {
        NSLog(@"Result : %d",result);
    }
    if (error) {
        NSLog(@"Error : %@",error);
    }
    [self dismissModalViewControllerAnimated:YES];

}

@end

Output

Now when we run the application we'll get the following output.
iOS Tutorial
On clicking send Email we will get the following output.
iOS Tutorial

0 comments: