开发手册 欢迎您!
软件开发者资料库

iOS - 发送电子邮件

iOS发送电子邮件 - 在iOS上学习iPhone和iPad应用程序开发,从简单和简单的步骤开始,从基本到高级概念,包括入门,环境设置,Objective-C,首个iPhone应用程序,操作和插座,代表,UI元素,加速度计,通用应用程序,相机管理,位置处理,SQLite数据库,发送电子邮件,音频和视频,文件处理,访问地图,应用程序内购买,iAd集成,GameKit,故事板,自动布局,Twitter和Facebook,内存管理,应用程序调试。

我们可以使用iOS设备的电子邮件应用程序发送电子邮件.

涉及的步骤

步骤1 : 创建一个简单的基于视图的应用程序.

步骤2 : 选择项目文件,然后选择目标,然后添加 MessageUI.framework .

步骤3 : 在 ViewController.xib 中添加一个按钮,然后创建一个发送电子邮件的操作.

步骤4 : 更新 ViewController.h 如下 :

#import #import @interface ViewController : UIViewController {   MFMailComposeViewController *mailComposer;}-(IBAction)sendMail:(id)sender;@end

第5步 : 更新 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

输出

当我们运行应用程序时,我们会得到以下输出 :

iOS Tutorial

点击发送电子邮件,我们将得到以下输出 :

iOS Tutorial