how send email by iPad - NSobject class - ios

I have project very simply as Unity projects, and I want add a new function - sending for email application screenshot,
I try many ways to do that, but I am neebie in IOS and need your help :(
This version is working without errors, but after click button I didnt see email form
code is very short and simple - I hope someone has help me :(((
SampleViewsAppDelegate.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <MapKit/MKMapView.h>
#import <MessageUI/MessageUI.h>
#import "tiDFusionMobile.h"
#interface SampleViewsAppDelegate : NSObject <UIApplicationDelegate,MFMailComposeViewControllerDelegate> {
///Application Window
UIWindow *mWindow;
UIViewController *rootViewController;
///Application views
UIView *mRender;
tiComponent* mPlayer;
}
///IBOutlet properties
#property (nonatomic, retain) IBOutlet UIWindow *mWindow;
#property (nonatomic, retain) IBOutlet UIViewController *rootViewController;
#property (nonatomic, retain) IBOutlet UIView *mRender;
- (IBAction)openMailBtn:(id)sender;
- (void)start;
- (void)stop;
#end
file mm
#import "SampleViewsAppDelegate.h"
#implementation SampleViewsAppDelegate
#synthesize mWindow;
#synthesize mRender;
#synthesize rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// allocate the Component
mPlayer = [tiComponent alloc];
// set correct renderer
[mPlayer setRendererType:[tiComponent TI_RENDERER_GLES2]];
// initialze
[mPlayer initialize:mRender];
// start scenario
[self start];
return YES;
}
- (void)dealloc
{
[self stop];
//If the player is still instanciated, it is terminated and released
if (mPlayer)
{
[mPlayer terminate];
[mPlayer release];
mPlayer = nil;
}
[mRender release];
[mWindow release];
[super dealloc];
}
- (IBAction)openMailBtn:(id)sender {
rootViewController = (UIViewController*)
[(SampleViewsAppDelegate*)[[UIApplication sharedApplication] delegate] rootViewController];
if ([MFMailComposeViewController canSendMail]) {
// compose
MFMailComposeViewController* mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
//format message
NSArray *recipientsArray = [[NSArray alloc] initWithObjects:#"test#aaaa.com", nil];
[mail setToRecipients:recipientsArray];
NSString *emailBody = #"DSDSDSDS";
[mail setSubject:[NSString stringWithFormat:#"AAAAAA"]];
//UIImage *myImage = [UIImage imageNamed:#"mobiletuts-logo.png"];
//NSData *imageData = UIImagePNGRepresentation(myImage);
//[mail addAttachmentData:imageData mimeType:#"image/png" fileName:#"mobiletutsImage"];
[mail setMessageBody:emailBody isHTML:YES];
//send
//if (controller)
[rootViewController presentModalViewController:mail animated:YES];
[mail release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
#pragma mark - MFMailComposeController delegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email message was queued");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved: you saved the email message in the Drafts folder");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send: the email message is queued in the outbox. It is ready to send the next time the user connects to email");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: the email message was nog saved or queued, possibly due to an error");
break;
default:
NSLog(#"Mail not sent");
break;
}
[self dismissViewControllerAnimated:YES complete:nil];
}
- (void)start
{
if (mPlayer != nil) {
BOOL isLoaded = [mPlayer loadScenario:#"Scenario/SampleViews_GLES1/project.dpd"];
if (isLoaded) {
[mPlayer playScenario];
}
}
}
- (void)stop
{
if (mPlayer && ![mPlayer isScenarioPaused]) {
[mPlayer pauseScenario];
}
}
#end

I GUESS YOU ARE NOT IMPORTING FRAMEWORK
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface MailClassViewController : UIViewController<MFMailComposeViewControllerDelegate>
And then the code to present the email screen:
- (IBAction)openMailBtn:(id)sender {
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self;
[mailCont setSubject:#"Your email"];
[mailCont setMessageBody:[#"Your body for this message is " stringByAppendingString:#" this is awesome"] isHTML:NO];
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"MaintenanceRequest.pdf"];
//IF YOU DONT WANT TO ATTACH FILE THEN COMMENT IT
NSData *data=[NSData dataWithContentsOfFile:file];
[mailViewController addAttachmentData:data mimeType:#"application/pdf" fileName:#"MaintenanceRequest.pdf"];
[self presentViewController:mailCont animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
//handle any error
[controller dismissViewControllerAnimated:YES completion:nil];
}

Related

How to send mail from a custom UITableViewCell using MFMailComposerViewController [duplicate]

This question already has answers here:
Objective C: Send email without leaving app
(3 answers)
Closed 3 years ago.
I want to send mail while clicking a button in a custom UITableViewCell using MFMailComposeViewController.
Appreciate if answer is in Objective-C.
Following code in your .h file
#import <MessageUI/MFMailComposeViewController.h>
Give Delegate <MFMailComposeViewControllerDelegate>
Following code in your .m file
//Where you want to open dialog write below code
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self; // Required to invoke mailComposeController when send
[mailCont setSubject:#"Your Subject!"];
[mailCont setToRecipients:[NSArray arrayWithObject:#"hello#test.com"]];
[mailCont setMessageBody:#"Your Body" isHTML:NO];
[self presentViewController:mailCont animated:YES completion:nil];
}
//Delegate Method
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
//YOUR ACTION
break;
case MFMailComposeResultSent:
//YOUR ACTION
break;
case MFMailComposeResultSaved:
//YOUR ACTION
break;
case MFMailComposeResultFailed:
//YOUR ACTION
break;
default:
break;
}
}
You can dismiss view by this code - [self dismissViewControllerAnimated:YES completion:nil];
Make sure you are testing it in Real device not in simulator and you have a mail ID configured in your device
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface ViewController ()<MFMailComposeViewControllerDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)sendmail:(id)sender {
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Subject"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"Recipients", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody = #"Body";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentViewController:mailer animated:YES completion:nil];
}
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// Close the Mail Interface
[self dismissViewControllerAnimated:NO completion:NULL];
}

How to send email in Xcode with Version 6.2?

I have looked at various tutorials for sending email in an app using Xcode, but most of the tutorials I have tried will not work for the current version of Xcode 6.2. When I run the IOS simulator, the email view will either instantly disappear or I can't type anything in any of the fields. I made sure to update the framework to use the MessageUI.framework. I will also post the current code I am using for the app. I want to use objective C as the language, as the app I am creating is using Objective C. Does anyone know how I can implement email in the current version(6.2)?
.h file
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface IntakeViewController : UIViewController <MFMailComposeViewControllerDelegate>{
}
- (IBAction)emailClicked:(id)sender;
#end
.m file
#import "IntakeViewController.h"
#interface IntakeViewController ()
#end
#implementation IntakeViewController
- (IBAction)emailClicked:(id)sender {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate = self;
[mail setSubject:#"Review"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"cbailey92#gmail.com", nil];
[mail setToRecipients:toRecipients];
NSString *body = #"You need more passion";
[mail setMessageBody:body isHTML:NO];
mail.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:mail animated:YES completion:nil];
} else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Run Away!" message:#"You need more passion" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Saved");
break;
case MFMailComposeResultFailed:
NSLog(#"Failed");
break;
case MFMailComposeResultSent:
NSLog(#"Sent");
break;
default:
NSLog(#"Default");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

Getting error with MFMailComposeViewControllerDelegate EXE_BAD_ACCESS (code=1, address=0x8)

I am trying to make mail composer as a helper class. It shows window when called from viewcontroller class viewDidLoad method. But when I press cancel and then Delete Draft. The app is crashing. After crashing its going to main file and showing following error message.
EXE_BAD_ACCESS (code=1, address=0x8).
I have tried all the possible things, but still didn't get any solution.
Any advice will be greatly appreciated! Thank you in advance.
Below are the code.
ViewController.m :-
- (void)viewDidLoad {
[super viewDidLoad];
//Allocating mailing object.
EmailManagerDelegate *MailOperation=[[EmailManagerDelegate alloc] init];
//Email Subject
MailOperation.emailTitle=#"just trying mail composer";
//Email content.
MailOperation.messageBody=#"hallo world";
//To address
MailOperation.toRecipent=[NSArray arrayWithObject:#"abc#gmail.com"];
//Setting the name of the File with Format in nstring.
MailOperation.FileNameWithFormat=#"Demo.txt";
//Passing the UIViewCOntroller for opeing the mailclient and closing it.
MailOperation.ViewVontroller=self;
[MailOperation emailMultiAttachAndSendLog:nil fileName: nil];
}
EmailManagerDelegate.h :-
#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>
#interface OSMEmailManagerDelegate : NSObject <MFMailComposeViewControllerDelegate>
#property (readwrite) NSString* FileNameWithFormat;
#property (nonatomic, strong) UIViewController* ViewVontroller;
//#property (nonatomic) UIViewController* ViewVontroller;
#property (nonatomic,strong) MFMailComposeViewController *mc;
#property (readwrite)NSString* emailTitle;
#property (readwrite)NSString* messageBody;
#property (readwrite)NSArray* toRecipent;
-(void)emailMultiAttachAndSendLog:(NSString*)documentsDirectory fileName:(NSString*)fileWithFormat;
EmailManagerDelegate.m :-
#import "OSMEmailManagerDelegate.h"
#implementation OSMEmailManagerDelegate
#synthesize mc,ViewVontroller;
-(void)emailMultiAttachAndSendLog:(NSString*)documentsDirectory fileName:(NSString*)fileWithFormat{
if ([MFMailComposeViewController canSendMail])
// The device can send email.
{
//Creating a mail composer
mc=[[MFMailComposeViewController alloc] init];
[mc setMailComposeDelegate:self];
[mc setSubject:_emailTitle];
[mc setMessageBody:_messageBody isHTML:NO];
[mc setToRecipients:_toRecipent];
[mc setModalPresentationStyle: UIModalPresentationFormSheet];
NSData *fileData;
NSString *mimeType;
NSString *filesName;
if (documentsDirectory && fileWithFormat) {
//Files section
NSArray *filePart=[fileWithFormat componentsSeparatedByString:#"."];
filesName=[filePart objectAtIndex:0];
NSString *fileExtention=[filePart objectAtIndex:1];
//Getting and creating path of the zip file.
NSString *fileArchivePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, fileWithFormat];
//Geting the resource file and path
fileData=[NSData dataWithContentsOfFile:fileArchivePath];
//Determining the MIME type.
mimeType=[self getMIMEType:fileExtention];
//Add attachment
[mc addAttachmentData:fileData mimeType:mimeType fileName:filesName];
}
//Present mail view controller on screen.
[ViewVontroller presentViewController:mc animated:YES completion:nil];
}
else
// The device can not send email.
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:#"Device not configured to send mail." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
//Get the type of MIME with file extention.
-(NSString *)getMIMEType:fileExtention{
NSString *mimeType=#"";
if ([fileExtention isEqualToString:#"csv"]) {
mimeType=#"text/csv";
}
else if ([fileExtention isEqualToString:#"jpg"]) {
mimeType = #"image/jpeg";
} else if ([fileExtention isEqualToString:#"png"]) {
mimeType = #"image/png";
} else if ([fileExtention isEqualToString:#"doc"]) {
mimeType = #"application/msword";
} else if ([fileExtention isEqualToString:#"ppt"]) {
mimeType = #"application/vnd.ms-powerpoint";
} else if ([fileExtention isEqualToString:#"html"]) {
mimeType = #"text/html";
} else if ([fileExtention isEqualToString:#"pdf"]) {
mimeType = #"application/pdf";
} else if ([fileExtention isEqualToString:#"json"]) {
mimeType = #"text/json";
} else if ([fileExtention isEqualToString:#"zip"]) {
mimeType = #"application/zip";
}
return mimeType;
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
NSString *status;
switch(result){
case MFMailComposeResultCancelled:
status=#"Mail cancelled";
break;
case MFMailComposeResultSaved:
status=#"Mail saved";
break;
case MFMailComposeResultSent:
status=#"Mail sent";
break;
case MFMailComposeResultFailed:
status=#"Mail sent failure : %#",[error localizedDescription];
break;
default:
break;
}
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:status delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
//Close mail interface.
[ViewVontroller dismissViewControllerAnimated:YES completion:NULL];
}
You can't have the mailComposeController dismiss itself. You have to have the UIViewController that presents it to dismiss it.
You need to move the delegation method to your UIViewController...
Here's my summerised answer to a re-write of your code:
#interface UIViewController : UIViewController <MFMailComposeViewControllerDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
//Allocating mailing object.
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
if ([MFMailComposeViewController canSendMail])
// The device can send email.
mc=[[MFMailComposeViewController alloc] init];
[mc setMailComposeDelegate:self];
[mc setSubject:#"hello world"];
[mc setMessageBody:#"hello world" isHTML:NO];
[mc setToRecipients:[NSArray arrayWithObject:#"abc#gmail.com"]];
[mc setModalPresentationStyle: UIModalPresentationFormSheet];
// This is the data you could handle in your custom class... I am leaving this code as is, fix it in the way your logic works.
[MailOperation emailMultiAttachAndSendLog:nil fileName: nil];
//Present mail view controller on screen.
[self presentViewController:mc animated:YES completion:nil];
} else {
// The device can not send email.
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:#"Device not configured to send mail." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
NSString *status;
switch(result){
case MFMailComposeResultCancelled:
status=#"Mail cancelled";
break;
case MFMailComposeResultSaved:
status=#"Mail saved";
break;
case MFMailComposeResultSent:
status=#"Mail sent";
break;
case MFMailComposeResultFailed:
status=#"Mail sent failure : %#",[error localizedDescription];
break;
default:
break;
}
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:status delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
//Close mail interface.
[self dismissViewControllerAnimated:YES completion:NULL];
}
EmailManagerDelegate.h :- (Don't call it Delegate... it's not what a delegate it... refactor it and remove the word delegate)
#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>
#interface OSMEmailManagerDelegate : NSObject
#property (readwrite) NSString* FileNameWithFormat;
// #property (nonatomic, strong) UIViewController* ViewVontroller;
//#property (nonatomic) UIViewController* ViewVontroller;
//#property (nonatomic,strong) MFMailComposeViewController *mc;
//#property (readwrite)NSString* emailTitle;
//#property (readwrite)NSString* messageBody;
//#property (readwrite)NSArray* toRecipent;
//-(void)emailMultiAttachAndSendLog:(NSString*)documentsDirectory fileName:(NSString*)fileWithFormat;
EmailManagerDelegate.m :- (Again, highly suggested to remove the 'delegate' part)
#import "OSMEmailManagerDelegate.h"
#implementation OSMEmailManagerDelegate
//Get the type of MIME with file extention.
-(NSString *)getMIMEType:fileExtention{
NSString *mimeType=#"";
if ([fileExtention isEqualToString:#"csv"]) {
mimeType=#"text/csv";
}
else if ([fileExtention isEqualToString:#"jpg"]) {
mimeType = #"image/jpeg";
} else if ([fileExtention isEqualToString:#"png"]) {
mimeType = #"image/png";
} else if ([fileExtention isEqualToString:#"doc"]) {
mimeType = #"application/msword";
} else if ([fileExtention isEqualToString:#"ppt"]) {
mimeType = #"application/vnd.ms-powerpoint";
} else if ([fileExtention isEqualToString:#"html"]) {
mimeType = #"text/html";
} else if ([fileExtention isEqualToString:#"pdf"]) {
mimeType = #"application/pdf";
} else if ([fileExtention isEqualToString:#"json"]) {
mimeType = #"text/json";
} else if ([fileExtention isEqualToString:#"zip"]) {
mimeType = #"application/zip";
}
return mimeType;
}
The following line causes the exception.
EmailManagerDelegate *MailOperation=[[EmailManagerDelegate alloc] init];
Notice that MailOperation is a local variable, which only lives in viewDidLoad, so when you click the send button, it no longer exists.
Changing it to a global variable inside ViewController should work.

How to attach a PDF file to mail and send mail in iOS

Currently I am developing an expense management app and I want to attach a PDF file to the mail and send it to the specified email Address. So guide me from where should I start this and how to develop it.
Thanks.
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
vc.mailComposeDelegate = self;
[vc setSubject:#"Monthly Expense Report"];
[vc addAttachmentData:pdfData mimeType:#"application/pdf" fileName:[NSString stringWithFormat:#"%#.pdf",[arrOfExpenseDate objectAtIndex:1]]];
if ([MFMailComposeViewController canSendMail])
{
[self presentViewController:vc animated:YES completion:nil];
}
I have created a example for you on how to send an pdf attachment using MFMailComposeViewController. Following is the code :
#interface ViewController () <MFMailComposeViewControllerDelegate>
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)compseEmail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
vc.mailComposeDelegate = self;
[vc setSubject:#"Monthly Expense Report"];
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"ApacheInstallationSteps" ofType:#"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
[vc addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"ApacheInstallationSteps.pdf"];
[self presentViewController:vc animated:YES completion:nil];
}
else
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Alert!!!" message:#"Your device can't send emails." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(#"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(#"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Result: failed");
break;
default:
NSLog(#"Result: not sent");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
Make sure that you add MessageUI.framework. Following is the output.
To send a pdf or any other attahcment in mail using MFMailComposeViewController, you have to use:
[mc addAttachmentData:fileData mimeType:mimeType fileName:filename];
For more details, follow this tutorial.
You can also download the sample code given in this tutorial for better understanding.
Hope that helps.

How to send an E-Mail inside of an app in Xcode?

I am new to xcode, and am wondering how to send email in an app! My code is below, but I keep getting the error "No visible #interface for 'jakem' declares the selector 'presentViewControllerAnimated:'". Is my code completely wrong? Or did I just forget to declare the selector, and how do I declare the selector? I have researched all over the internet for at least an hour, and nothing is working. Someone please help me!
-(IBAction)sendEmail{
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"FrankMurphy.CEO#RomansXIII.com", nil]];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:composer animated:YES];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
if(error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"error" message:[NSString stringWithFormat:#"error %#", [error description]] delegate:nil cancelButtonTitle:#"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissViewControllerAnimated:YES];
}
else {
[self dismissViewControllerAnimated:YES];
}
}
in .h header file....
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface SimpleEmailViewController : UIViewController <MFMailComposeViewControllerDelegate> // Add the delegate
- (IBAction)showEmail:(id)sender;
#end
in .m implementation file.....
- (IBAction)showEmail:(id)sender {
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"info#finetechnosoft.in"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
Check if you are MFMailComposeViewControllerDelegate.
You do this like
#interface YouClassName : UIViewController <MFMailComposeViewControllerDelegate>
#end
I think you're using the wrong method. Try
[self presentViewController:(UIViewController *) animated:(BOOL) completion:(void)completion];
instead of:
[self presentViewController:composer animated:YES];
I work for Sendgrid. We have an Objective-c library that lets you quickly send email from inside your app, https://github.com/sendgrid/sendgrid-objc. You can use cocoapods to quickly install the library in your project.
Then sending the email from your (IBAction) would look like this:
-(IBAction)sendEmail{
sendgrid *msg = [sendgrid user:#"username" andPass:#"password"];
msg.to = #"FrankMurphy.CEO#RomansXIII.com";
msg.from = #"me#bar.com";
msg.text = #"hello world";
msg.html = #"<h1>hello world!</h1>";
[msg sendWithWeb];
}

Resources