Follow the following step to send email in iOS Objective C-
Step - 1 Include the MessageUI.framework in your project . Select project go ->settings ->General->Linked Frameworks and Libraries. Tap on + and select MessageUI.framework . Following is the screen shot -
Step 2- #import <MessageUI/MessageUI.h> in your UIViewController.h file. Here is my view controller.h and viewcontroller.m file-
//
// ViewController.h
// LoginSample
//
// Created by MSClient Mac01 on 10/01/17.
// Copyright © 2017 Mindstick Software. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>
- (IBAction)sendMail:(id)sender;
@end
//
// ViewController.m
// LoginSample
//
// Created by MSClient Mac01 on 10/01/17.
// Copyright © 2017 Mindstick Software. All rights reserved.
//
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
appDelegate = (AppDelegate *)[[UIApplicationsharedApplication] delegate];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)sendMail:(id)sender {
MFMailComposeViewController *emailController = [[MFMailComposeViewControlleralloc] init];
emailController.mailComposeDelegate = self;
// [emailController setSubject:@"Sample email subject "]; // set default subject
// [emailController setMessageBody:@"Hi , this is sample email body" isHTML:NO]; // set Yes if contains html
[selfpresentViewController:emailController animated:YEScompletion:NULL];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result) {
caseMFMailComposeResultSent:
NSLog(@"E-mail sent successfully.");
break;
caseMFMailComposeResultSaved:
NSLog(@"E-mail saved.");
break;
caseMFMailComposeResultCancelled:
NSLog(@"E-mail cancelled.");
break;
caseMFMailComposeResultFailed:
NSLog(@"E-mail could not be sent.");
break;
default:
break;
}
[controller dismissViewControllerAnimated:YEScompletion:nil];
}
@end
Following is the screen shot of MFMailComposeViewController -
Leave Comment