在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. js 基础笔记三

    词法结构: 1:区分大小写 2:特殊字符的区分,unicode转义 3:注释, //  ;  /* */ ; 4 : 标识字符和保留字 数据类型: 1原始类型 数字,字符串,布尔值.特殊的原始值(nu ...

  2. 转:Jmeter进行分布式性能测试

    由于Jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起JAVA内存溢出的错误.要解决这个问题,可以使用分布式测试,运行多台机器运行所谓的 ...

  3. MySQL常用命令总结2

    USE db_name; //使用(打开)数据库 SELECT DATABASE(); //查看当前打开的数据库 CREATE TABLE tb_name( column_name data_type ...

  4. Redis(2)用jedis实现在java中使用redis

    昨天已经在windows环境下安装使用了redis. 下面准备在java项目中测试使用redis. redis官网推荐使用jedis来访问redis.所以首先准备了jedis的jar包,以及需要依赖的 ...

  5. 求交集,差集,并集,善用java的set

    当有题目有求这些结果时,使用集合数据结构还是很快的.需要考虑的是,注意map和set的区别. public static void main(String[] args) { Set<Integ ...

  6. hdu_3886_Final Kichiku “Lanlanshu”(数位DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3886 题意:这题的题意有点晦涩难懂,大概意思就是给你一个区间,让你找一些满足递增递减条件的数,举个列: ...

  7. Excel相关问题

    Excel默认永远使用最后安装的那个Excel版本打开.但是如果有一个Excel已经启动了,则使用那个Excel打开. 1.打开“开发工具”选项卡2007中:[Excel选项]-[常用]2010中:[ ...

  8. MyEclipse2015 javaweb项目从svn检出后变成java项目,clean之后不能编译,解决办法是

     javaweb项目从svn检出后变成java项目,解决办法是:1.项目右键–properties–Project Facets,勾选上Dynamic Web Module .Java 两个复选框.点 ...

  9. URL 传+号到后台变空格问题解决方案

    今天巧合遇到这个问题,下面是网上找的解决方案. 原文:http://blog.sina.com.cn/s/blog_a0949eec01010xta.html 今天在调试客户端向服务器传递参数时,参数 ...

  10. 2016大连网络赛 Different GCD Subarray Query

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...