Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 03-21-2010, 03:50 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 24
iamsgtyang is on a distinguished road
Default Quick Tutorial on how to add MFMailComposeViewController

This process is fairly simple. The SDK includes the MessageUI.framework which simplifies this process to a few lines of code; don't know why you would want to send an email from the iPhone in another way.

1. Create a new View-Based Application and name it MailTutorial.

2. Import the framework “MessageUI.framework” into the project.

3. Add these lines into the MailTutorialViewController.h file.

Code:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface MailTutorialViewController : UIViewController <MFMailComposeViewControllerDelegate>{
}

- (IBAction)actionEmailComposer;

@end
Here we need to import <MessageUI/MessageUI.h> and the <MessageUI/MFMailComposeViewController.h>. We added the MessageUI.framework and we needed to import these two files from the framework into our viewcontrollers header. We also need to add the < MFMailComposeViewControllerDelegate>.

Now, the IBAction class method named actionEmailComposer is what’s important. You can call this method however you want in your program. Here we’re going to call this method with the UIButton created in Interface Builder to simplify it. (Save all files. Open up MailTutorialViewController.xib with Interface Builder. Drag a UIButton onto the view. Give the UIButton a title called “Mail Composer” and then click on the Connections Inspector tab. Drag “Did Touch Up Inside” to File’s Owner in the Document window and link it to actionMailComposer. Save and quit.)

4. Now add these lines into the MailTutorialViewController.m file.

Code:
#import “MailTutorialViewController.h”

@implementation MailTutorialViewController

- (void)viewDidLoad {

[super viewDidLoad];

}

- (IBAction)actionEmailComposer {

if ([MFMailComposeViewController canSendMail]) {

MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:@"Subject Goes Here."];
[mailViewController setMessageBody:@"Your message goes here." isHTML:NO];

[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];

}

else {

NSLog(@”Device is unable to send email in its current state.”);

}

}

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult MFMailComposeResult)result error  NSError*)error {

[self dismissModalViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

- (void)viewDidUnload {

}

- (void)dealloc {

[super dealloc];

}
@end
Here we added the IBAction method code. First we wanted to check if the current device’s state is able to send email so we are using a *if* function here to simply test it. MFMailComposeViewController has a method called canSendMail that returns a boolean value of YES if the device is configured for sending email or NO if it is not.

If the device is able to send emails, we created mailViewController by initializing and allocating MFMailComposeViewController and delegating the mailComposeDelegate to self.

To set the subject and body of the e-mail message, we used the setSubject and setMessageBody instance method. The class reference for the MFMailComposeViewController can be found here iPhone Dev Center: MFMailComposeViewController Class Reference

I set the isHTML for the message body to NO. You can use HTML if you want by setting it to YES.

We are presenting the mailViewController, the MFMailComposeViewController allocated, with as a modal view with presentModalViewController so when the button is clicked the mailViewController slides up to fill the screen.

The MFMailComposeViewControllerDelegate protocol has a call-back method “mailComposeController:didFinishWithResult:error:” and this method indicates if the user is finished with the mailComposerView, the MFMailComposeViewController. MFMailComposeResult provides the vales of the action of the email composer: MFMailComposeResultSent, MFMailComposeResultSaved, MFMailComposeResultCancelled and MFMailComposeResultFailed. To get more information about any failures, you can retrieve the error parameter.

Here I just want to dismiss the ModalViewController and nothing else so I’m just calling dismissModalViewControllerAnimated.

Build and Run and check it out. This is pretty simple for sending a e-mail with your iPhone with just a subject and message. Check out the class reference for MFMailComposeViewController for additional goodies.

I have the source and same step on my site here.

I haven't worked much outside of basic email on the iPhone because I haven't needed to. If you know more or can help improve this, let me know. Thanks.
iamsgtyang is offline   Reply With Quote
Old 03-21-2010, 03:51 PM   #2 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
ZunePod is on a distinguished road
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

You might want to do it manually if you have a bug report thing, or support thing isnide your app.
__________________
Will code for food
ZunePod is offline   Reply With Quote
Old 03-21-2010, 04:04 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 24
iamsgtyang is on a distinguished road
Default

Ah, I never had to work in those situations before. I would just present the user with the same information filled out in the body. This gives them the option to send the report or feedback and look through the message to see what they are actually sending.

I think I might look into sending without MFMail...
iamsgtyang is offline   Reply With Quote
Old 04-24-2010, 04:48 PM   #4 (permalink)
Registered Member
 
finefin's Avatar
 
Join Date: Jun 2009
Posts: 259
finefin is on a distinguished road
Default

Quote:
Originally Posted by iamsgtyang View Post
Thank you very much! It works like a charm -- although I had to take a look to your source code to find the error the compiler kept on throwing at me:

Code:
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult MFMailComposeResult)result error  NSError*)error {
        [self dismissModalViewControllerAnimated:YES];
}
naturally should be:

Code:
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
	[self dismissModalViewControllerAnimated:YES];
}
Looks like the forum misinterpreted the ": (" for a sad smiley ---
but look at me, I am totally now

cheers
__________________

I help you, you buy my app - you help me, I buy your app

PARALLAX, The Book Of Sayings - chopped up edition! (HD),omicron,
Canupa Band deluxe (universal), Canupa Band FREE!, Your Face --- follow me on twitter
finefin is offline   Reply With Quote
Old 07-05-2010, 07:10 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 1
swarnava is on a distinguished road
Smile A New Problem:

that was ok,but my prob is i want to make that 'cc row' hidden from the screen,what should i do???i need this urgent!![]
swarnava is offline   Reply With Quote
Old 07-31-2010, 10:14 AM   #6 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 16
cwiedel is on a distinguished road
Default

I've run into a strange problem with this and I'm hoping someone can shed some light on it.

I set up my app to allow sending e-mail just like this. It works perfectly in the iPhone Simulator. However, when using an actual device (iPod running 3.1.3 in this case), the app winds up sort of stuck. The e-mail view comes up with the subject set, but the body isn't set, and the view is unresponsive. After a few minutes, it can detect a click on the cancel button and I can escape. Aside from that, I can't do anything.

Any ideas?
cwiedel is offline   Reply With Quote
Old 08-08-2010, 03:11 PM   #7 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 14
HopeMedia is on a distinguished road
Default how to mail a pdf from a website

I have a program i have made it has a UIview and browses to a website with a few steps the onloine program creates a .pdf file and i want an iphone to be able to email that pdf that is displayed on the webpage is there a way to do that easily?
HopeMedia is offline   Reply With Quote
Old 08-11-2010, 03:48 PM   #8 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 3
Roland9 is on a distinguished road
Default Thanks for this Tutorial

thanks for sharing, this is a very helpful tutorial, works like a charm
Roland9 is offline   Reply With Quote
Old 08-19-2010, 09:44 AM   #9 (permalink)
I will be a billionaire.
 
IphoneSdk's Avatar
 
Join Date: Sep 2009
Location: Scarborough, United Kingdom
Age: 17
Posts: 1,344
IphoneSdk is on a distinguished road
Smile

Nice Tutorial!
__________________
iOS Apps | Website | Twitter | Facebook
IphoneSdk is online now   Reply With Quote
Old 12-12-2011, 12:21 AM   #10 (permalink)
Registered Member
 
Join Date: Sep 2011
Location: texas
Posts: 49
raptor85 is on a distinguished road
Default

Quote:
Originally Posted by IphoneSdk View Post
Nice Tutorial!
i want to create something similar to this
hi i am creating an app where there is an email label(example@xyz.com) by pressing that label a pop over will come where it asks for user to enter his email id , subject (textview) , after user enters the details there will be submit button ,when the user clicks the submit button ,(example@xyz.com) will receive the email. i wanted to know how to do it ,any help would be helpful
raptor85 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,959
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0