简介
我们可以使用电子邮件应用程序 iOS 设备发送电子邮件(Email)。
涉及的步骤
1. 创建一个简单的 View based application.
2. 选择项目文件,然后选择目标,添加 MessageUI.framework.
3. 在 ViewController.xib 添加一个按键并创建一个用于发送电子邮件的动作。
4. 更新 ViewController.h 如下
#import <UIKit/UIKit.h> #import <MessageUI/MessageUI.h> @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate> { MFMailComposeViewController *mailComposer; } -(IBAction)sendMail:(id)sender; @end
4. 更新 ViewController.m 如下
#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
输出
现在,当我们运行程序时,我们会得到下面的输出。
在点击“发送电子邮件,我们将得到下面的输出。