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. 【bzoj5177】[Jsoi2013]贪心的导游(分块)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5177 在网上看到的题解基本都是用主席树,也就是带点骚操作的暴力直接艹过去的.这里分享一 ...

  2. Sqoop-问题

    1.权限问题 // :: INFO mapreduce.Job: Job job_1555064328415_0328 failed with state FAILED due to: Job set ...

  3. 【Semantic Segmentation】Segmentation综述

    部分转自:https://zhuanlan.zhihu.com/p/37618829 一.语义分割基本介绍 1.1 概念 语义分割(semantic segmentation) : 就是按照" ...

  4. Visual Studio 2010 C++ 属性设置基础

    在 <Visual Studio 2010 C++ 工程文件解读>中提到了C++工程中可以进行用户自定义的属性设置,如何进行属性设置呢? 下面我们来了解一下 props 文件的基本规则: ...

  5. vue的seo方案 prerender-seo-plugin

    利用vue cli 3.0安装脚手架.记住:勾选vue-router. 在vue.config.js里添加配置: 2, var path = require('path') 3, const Prer ...

  6. 微信公众平台开发(一) 配置接口与验证URL

    一.简介 微信公众平台是腾讯公司在微信的基础上新增的功能模块,通过这一平台,个人和企业都可以打造一个微信的公众号,并实现和特定群体的文字.图片.语音的全方位沟通.互动. 二.通讯机制 三.注册微信平台 ...

  7. 引发事件代码封装成OnEventName

    引发事件的代码,通常可以封装成“On+事件名称”的方法(On:表示当“什么什么”的时候),如下所示: 1:引发事件代码: if (PropertyChanged != null)//为了实现将数据源的 ...

  8. 作业列表 of《软件测试技术》

    作业1(截止时间3月22日) 请使用excel模板或word模板,完成对126邮箱登录功能的测试用例编写,界面如下图.提交到ftp. --------------------------------- ...

  9. cJONS序列化工具解读三(使用案例)

    cJSON使用案例 由了解了cJSON的数据结构,接口以及实现之后,那么我们来举例说明其使用. 本例子是一个简单的学生信息表格管理,我们通过键值对的方式向json中增加元素信息. 然后可以格式化输出结 ...

  10. CSU-1307-二分+dij

    1307: City Tour Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 59 ...