Quick Tutorial on how to add MFMailComposeViewController
来源:互联网 发布:网络教育会计专科 编辑:程序博客网 时间:2024/06/14 02:00
http://www.iphonedevsdk.com/forum/tutorial-discussion/43633-quick-tutorial-how-add-mfmailcomposeviewcontroller.html
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.
#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.
#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.
- Quick Tutorial on how to add MFMailComposeViewController
- How to add extended Quick Settings
- Quick Tip: How to Add Syntax Highlighting to Any Project
- how to add syscall on x86_64
- How To Add Swap on CentOS 6
- How to add start menu on Ubuntu
- Tutorial on how to install apache spark on Windows
- How to Install Kali Linux on Android - Tutorial With Screenshot
- How to add buttons (Home, Back, Menu) on Android statusBar
- How to Add Virtual Keys on Status Bar
- How to Install OracleClient11g32-bit on Win7 + Add ODBC
- How to add a Primary Key on a Oracle view
- how to add a swap file on linux
- How to add a new key on android
- How to add ppa repository on kali linux
- [BT]How to add BT Address on MT2523
- how to add lun
- [Ubuntu]--add-apt-repository/How to add a PPA on a server?
- shell作业与答案
- IMP-00013: 只有 DBA 才能导入由其他 DBA 导出的文件
- 关于运行时共享库的问题
- 通过无线网卡在数据链路层进行通信
- ……
- Quick Tutorial on how to add MFMailComposeViewController
- Shell.Application技术资料
- Binary Log
- struts中获取多选按钮html:checkbox的值
- Transaction
- ACCESS数据库C#操作类(包含事务)
- The Portable Executable File Format from Top to Bottom
- 发现一篇不错的文章,包括ANR,Activity超负荷...等
- 网络编程之基础知识