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. LeetCode——Median of Two Sorted Arrays

    Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...

  2. layer弹出层的关闭及父页面的刷新问题

    当在主页面执行添加或修改时,用弹出层是比较好的选择,如何关闭弹出层并对父级页面进行操作呢 首先在父级页面中打开一个添加页面(弹出层) 在添加页面的表单提交函数中添加如下代码: function for ...

  3. 《Django By Example》

    <Django By Example>第六章 中文 翻译 (个人学习,渣翻) 书籍出处:https://www.packtpub.com/web-development/django-ex ...

  4. 获取远程html

    /// <summary> /// 获取远程html /// </summary> /// <param name="url"></par ...

  5. 解决httpclient请求响应压缩文本乱码问题

    最近在调用京东的获取省份接口老是中文乱码,加了utf-8也没有用.最后在httpclient打的日志中有Content-Encoding:gzip信息,最后在请求header里加上: reqHeade ...

  6. sass快速入门 - 笔记

    一.使用变量 1.使用$符号来标识变量. 例: $nav-color:#F90; .nav{ $width:100px; width:$width; color:$nav-color; }

  7. cygwin下安装软件

    cygwin下安装软件cygwin工具安装新的软件和常见的命令windows8.1下安装Cygwin并通过apt-cyg安装软件包Cygwin利用apt-cyg安装gcc.g++.make和gdb 首 ...

  8. 英语词根与单词的说文解字---词根示例1、第10页 st(at)

    英语词根与单词的说文解字---词根示例1.第10页 st(at) 一.总结 一句话总结: 站 state,establish,constitution 英 [ɪ'stæblɪʃ; e-]  美 [ɪˈ ...

  9. 《Think in Java》(十三)字符串

    学完这章后,对 Java 字符串有了重新的认识.自己也看了下 CharSequence,String,StringBuilder,StringBuffer 等类的实现代码.

  10. 《高级Web应用程序设计》课件(20170911)

    第一阶段:千里之行,始于足下 第1章 ASP.NET MVC概述 第2章 音乐商店制作 第二阶段:欲穷千里目,更上一层楼 第3章 设计模型 3.1 数据模型概述 3.2 使用EF Code First ...