在IOS开发中,有时候我们会需要用到邮件发送的功能。比如,接收用户反馈和程序崩溃通知等等,这个功能是很常用的。在苹果系统中,如果彼此的手机都是iOS设备,并且开通了iMessage功能,那么彼此之间的短信是走网络通道,而不走运营商的通道,短信也顺便写写喽。

  还是老规矩,直接上代码。

//

//  ViewController.m

//  Demo-testEmail

//

//  Created by yyt on 16/5/16.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import "ViewController.h"

#import <MessageUI/MessageUI.h>

@interface ViewController ()<UIActionSheetDelegate,MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

button.frame = CGRectMake(self.view.bounds.size.width/2 - 30, 200, 60, 40);

button.backgroundColor = [UIColor orangeColor];

[button setTitle:@"分享" forState:UIControlStateNormal];

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

[self.view addSubview:button];

}

- (void)clickButton {

UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"分享" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Email",@"Message", nil];

[actionSheet showInView:self.view];

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

if(buttonIndex==1){

if ([MFMessageComposeViewController canSendText]) {

[self sendSMS:@"I‘m using iHeper,it is great!" recipientList:nil];

} else {

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"系统短信不可用!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

[alert show];

}

}else if(buttonIndex==0){

if ([MFMailComposeViewController canSendMail]) { // 用户已设置邮件账户

[self sendEmailAction]; // 调用发送邮件的代码

} else {

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"系统邮箱未设置账号!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

[alert show];

}

}

}

- (void)sendEmailAction {

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

mc.mailComposeDelegate = self;

[mc setSubject:@"Hi!"];

[mc setMessageBody:@"I‘m using iHeper,it is great!" isHTML:NO];

[self presentViewController:mc animated:YES completion:nil];

}

//调用sendSMS函数,发送内容,收件人列表

- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients {

MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

if([MFMessageComposeViewController canSendText])

{

controller.body = bodyOfMessage;

controller.recipients = recipients;

controller.messageComposeDelegate = self;

[self presentViewController:controller animated:YES completion:nil];

}

}

// 处理发送完的响应结果

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

[self dismissViewControllerAnimated:YES completion:nil];

if (result == MessageComposeResultCancelled)

NSLog(@"Message cancelled");

else if (result == MessageComposeResultSent)

NSLog(@"Message sent");

else

NSLog(@"Message failed") ;

}

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

switch (result)

{

case MFMailComposeResultCancelled:

NSLog(@"Mail send canceled...");

break;

case MFMailComposeResultSaved:

NSLog(@"Mail saved...");

break;

case MFMailComposeResultSent:

NSLog(@"Mail sent...");

break;

case MFMailComposeResultFailed:

NSLog(@"Mail send errored: %@...", [error localizedDescription]);

break;

default:

break;

}

[self dismissViewControllerAnimated:YES completion:nil];

}

@end

iOS开发——发短信,邮件的更多相关文章

  1. IOS,发短信,发邮件,打电话

    今天把APP里常用小功能 例如发短信.发邮件.打电话.全部拿出来简单说说它们的实现思路. 1.发短信实现打电话的功能,主要二种方法,下面我就分别说说它们的优缺点.1.1.发短信(1)——URL // ...

  2. iOS - 打电话, 发短信

    电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...

  3. ios打电话发短信接口

    电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...

  4. iOS 打电话 发短信(转载)

    官方代码 发短息和邮件添加MessageUI.framework 库 发送信息 - (IBAction)showSMSPicker:(id)sender { // You must check tha ...

  5. iOS打电话,发短信,发邮件,打开网址

    //调用自带mail [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzl ...

  6. iOS打电话、发短信、发邮件功能开发

    本文转载至 http://www.lvtao.net/ios/506.html 今天把APP里常用小功能 例如发短信.发邮件.打电话.全部拿出来简单说说它们的实现思路. 1.发短信实现打电话的功能,主 ...

  7. iOS开发中打电话发短信等功能的实现

    在APP开发中,可能会涉及到打电话.发短信.发邮件等功能.比如说,通常一个产品的"关于"页面,会有开发者的联系方式,理想情况下,当用户点击该电话号码时,能够自动的帮用户拨出去,就涉 ...

  8. IOS 开发,调用打电话,发短信,打开网址

    IOS 开发,调用打电话,发短信,打开网址   1.调用 自带mail [[UIApplication sharedApplication] openURL:[NSURL URLWithString: ...

  9. iOS中如何切换到发短信、打电话、发邮件

    我们在做APP的时候,难免会遇到需要调用短信,电话等程序的时候.如美团. 当然,这些都只是一些简单的方法就可以实现,但是时间久了也会淡忘,所以想写这边博客.一是为了再捡起来复习一下,另一个相当于留个备 ...

随机推荐

  1. HDU 1540<线段树,区间并>

    题目连接 参考 题意: 维护各个点的连续的最大连续长度. 思路: 主要是维护一个区间的三个变量ll,f[i].l为起点向右的最大连续 长度,rl:f[i].r为起点向左的最大连续长度,ml:[l,r] ...

  2. javascript语句语义大全(1)

    1. <script></script> javascript标签对,所有javascript内容都会写在这个里面 2. var a=0; var a; 变量的声明,上面的是声 ...

  3. Struts的前世今身

    1.Struts1的运行原理 a.初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件( ...

  4. 转:selenium webdriver 执行javascript代码

    在用selenium webdriver 编写web页面的自动化测试代码时,可能需要执行一些javascript代码,selenium本身就支持执行js,我们在代码中import org.openqa ...

  5. HDU 5240 Exam

    The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest 2015ACM-ICPC上海大都会赛 签到题 #include< ...

  6. Angular服务器通信之:$http

    $http服务提供了浏览器XMLHttpRequest对象的封装,并且作为Angular中和后台服务通信的底层服务,在此之上Angular还提供了一个可选模块ngResource支持与RESTFul的 ...

  7. 고서--做完A之后做B, B受A影响

    1. 합격 소식을 듣고서 매우 기뻤어요.. 2. 친구하고 심하게 다투고서 마음이 안 좋았어요. 3. 급한 일을 먼저 끝내고서 이야기합시다.' 4. 창문을 열고서 상쾌한 공기를 마서 ...

  8. SQL求解两个时间差

    sql 求解两个时间差 SELECTDATEDIFF( Second, '2009-8-25 12:15:12', '2009-9-1 7:18:20') --返回相差秒数 SELECTDATEDIF ...

  9. C# 双引号的输出

    Console.WriteLine("\"a little list.\"");

  10. zf-关于统计分析表单导出(写这个的 太麻烦了)

    一个类里面写了2个一样的方法 如果是我 会重复利用 而不是这样写 今天改bug的时候我把一个类修改了2次 差点以为进错了类