1.iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面.

2.项目中需要添加MessageUi.framework。头文件加入MFMailComposeViewControllerDelegate。#import <MessageUI/MessageUI.h>

- (void)viewDidLoad

{

  // 实例化按钮用来调用邮箱

  UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];

  button.frame = CGRectMake(0, 40, 320, 50);

  [button setTitle: @"Mail" forState: UIControlStateNormal];

  [button addTarget: self action: @selector(sendEmailAction) forControlEvents: UIControlEventTouchUpInside];

  [self.view addSubview: button];

}

- (void)sendEmailAction

{

   Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

if (mailClass != nil)

{

if ([mailClass canSendMail])

{

[self displayComposerSheet ];

}else{

[self launchMailAppOnDevice];

}

}else {

[self launchMailAppOnDevice];

}

}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result

{

// [self dismissModalViewControllerAnimated:YES];

[self dismissViewControllerAnimated:YES completion:nil];

  NSString*msg;

  switch (result) {

  case MessageComposeResultCancelled:

    msg = @"发送取消";

    break;

  case MessageComposeResultSent:

    msg = @"发送成功";

    break;

  case MessageComposeResultFailed:

    msg = @"发送失败";

    break;

    default:

    break;

}

UIAlertView*alert = [[UIAlertView alloc] initWithTitle:nil

message:msg

delegate:nil

cancelButtonTitle:@"关闭"

otherButtonTitles:nil];

[alert show];

}

//email界面,界面添加于window上

-(void)displayComposerSheet

{

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

     [picker setAccessibilityLanguage:@"Chinese"];

if (!picker) {

   // 在设备还没有添加邮件账户的时候mailViewController为空,下面的present view controller会导致程序崩溃,这里要作出判断

    NSLog(@"设备还没有添加邮件账户");

  return;

}

   picker.mailComposeDelegate = self;

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {

UIView *barBgV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];

barBgV.backgroundColor = [UIColor colorWithRed:55/255.0 green:60/255.0 blue:100/255.0 alpha:1.0];

UIImage *barBgImg = [Tool convertViewToImage:barBgV];

[[picker navigationBar] setBackgroundImage:barBgImg forBarMetrics:UIBarMetricsDefault];

}

// Set up recipients

      NSArray *toRecipients = [NSArray arrayWithObject:staff.email];

      [picker setToRecipients:toRecipients];

//    NSString *path = [[NSBundle mainBundle] pathForResource:@"Icon@2x.png"

//                                                     ofType:nil

//                                                inDirectory:nil];

//        NSData *myData = [NSData dataWithContentsOfFile:path];

//        [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"icon"];

    [picker setMessageBody:nil isHTML:YES];

    [self presentViewController:picker animated:YES completion:^{

   picker.accessibilityElementsHidden = YES;

}];

  // [self presentModalViewController:picker animated:YES];

}

//跳转到系统email界面

-(void)launchMailAppOnDevice

{

    NSString *recipients = [NSString stringWithFormat:@"mailto:%@?&subject=",staff.email];

    NSString *email = recipients;

    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

}

#pragma mark - 代理

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

{

   //关闭邮件发送窗口

   [self dismissViewControllerAnimated:YES completion:nil];

   NSString *msg;

    switch (result)

    {

  case MFMailComposeResultCancelled:

msg = @"用户取消编辑邮件";

    break;

  case MFMailComposeResultSaved:

msg = @"用户成功保存邮件";

    break;

  case MFMailComposeResultSent:

   msg = @"发送成功";

      break;

  case MFMailComposeResultFailed:

msg = @"用户试图保存或者发送邮件失败";

    break;

    default:

    break;

}

[[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil]show];

}

设置中文:

1.可以在项目的list属性文件中设置Localization native development region的属性值  为:China;

2.可以在调用MFMailComposeViewController的xib中设置Localization(方法如下:找到对应xib文件,右击该文件选择Get Infoà [General选项卡下单击{add Localization}])如中文简体{zh_CN}

3.或者在自己手机:设置-->通用--->多语言环境-->语言--->简体中文;

General--->International-->Language--->简体中文;

MFMailComposeViewController发送邮件的更多相关文章

  1. MFMailComposeViewController发送邮件的实例

    本文转载至 http://blog.csdn.net/liufeng520/article/details/7585140   iPhone API已经提供了系统写邮件界面的接口,使用MFMailCo ...

  2. 发送邮件(E-mail)方法整理合集

    在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原生自带 ...

  3. iOS-----MFMessageCompose 和 MFMailComposeViewController的使用方法

    MFMessageCompose 和 MFMailComposeViewController的使用方法 使用MFMessageComposeViewCOntroller发短信 应用想自己提供界面让用户 ...

  4. iOS开发-发送邮件(E-mail)方法整理合集(共3种)

    前言:在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原 ...

  5. iOS调用系统功能发邮件

    使用MFMailComposeViewController发送邮件 1.项目需要导入框架:MessageUI.framework 2.使用的Controller的.h文件中添加代理 MFMailCom ...

  6. iOS开发-邮件发送

    Web开发的时候邮箱注册登录是必不可少的,手机号可以更换,不过相对而言,邮箱只是用于比较重要的时候用到,比如找工作的时候必填的邮箱,注册网站会员的邮箱验证.现在的手机和Web的其实操作是一样的,大多数 ...

  7. 《疯狂iOS讲义(下)——iPhone/iPad高级应用与手游开发(含CD光盘1张)》

    <疯狂iOS讲义(下)——iPhone/iPad高级应用与手游开发(含CD光盘1张)> 基本信息 作者: 李刚    肖文吉 出版社:电子工业出版社 ISBN:9787121224379 ...

  8. 发送邮件--MFMailComposeViewController

    只能在真机使用. 模拟器没有E-mail发送功能.无法调用 #import "EmailViewController.h" #import <UIKit/UIKit.h> ...

  9. mono中发送邮件并保存本次收件人的地址

    在ios端mono开发中,发送邮件可以选择调用ios原生email程序.有两种方式实现这种功能,一是程序跳转到ipad中email程序,另外一种是将发送邮件的界面在自己应用里弹出. 首先第一种方式的代 ...

随机推荐

  1. Linux 下 YUM 安装 PHP 5.5 (及5.6)

    原文链接: http://blog.aboutc.net/linux/50/yum-install-php-on-linux 系统环境: CentOS 6.4 x86_64 Fedora 20 x86 ...

  2. 从0开始 数据结构 AC自动机 模板(from kkke)

    AC自动机模板 2.4.1 头文件&宏&全局变量 #include <queue> #define MAXN 666666 #define MAXK 26//字符数量 st ...

  3. HDU5299 圆的扫描线 && 树上删边博弈

    HDU5299 圆的扫描线 && 树上删边博弈 标签(空格分隔): 未分类 给出若干个圆,可以互相嵌套但不相交或相切. 每次删去一个圆和它内部的圆,进行博弈,问谁赢. 分成两部分.首先 ...

  4. mysql 索引相关问题

    mysql中key .primary key .unique key 与index区别 https://blog.csdn.net/nanamasuda/article/details/5254317 ...

  5. CALL_AND_RETRY_LAST Allocation failed node打包报错

    全局安装increase-memory-limit: npm install -g increase-memory-limit 进入工程目录,执行: increase-memory-limit

  6. 百度编辑器(ueditor)@功能之获取坐标

    //获取百度编辑器的工具类 var domUtils = UE.dom.domUtils; //获取编辑器的坐标 var $ueditor_offset = $("#ueditor_0&qu ...

  7. PHP表单(get,post)提交方式

    PHP 表单处理 PHP 超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data). $_GET 是通过 URL 参数传递到当前脚本的变量数组. $_POST 是通过 HTTP ...

  8. brew || yarn 软件包管理工具

    1.brew || yarn 软件包管理工具

  9. BZOJ 1005 [HNOI2008]明明的烦恼 ★(Prufer数列)

    题意 N个点,有些点有度数限制,问这些点可以构成几棵不同的树. 思路 [Prufer数列] Prufer数列是无根树的一种数列.在组合数学中,Prufer数列是由一个对于顶点标过号的树转化来的数列,点 ...

  10. 转:聊聊Greenplum的那些事

    笔者有幸从04年就开始从事大规模数据计算的相关工作,08年作为Greenplum 早期员工加入Greenplum团队(当时的工牌是“005”,哈哈),记得当时看了一眼Greenplum的架构(嗯,就是 ...