在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. GCD应用场景

    1.计算文件大小放在子线程中中计算,计算完了,回到主线程更新UI

  2. FTP: Configuring server users..

    4 points to create a user to uploade to ftproot.. this user must be an administrator, and be able to ...

  3. org.apache.lucene.queryParser.ParseException: Encountered "<EOF>" at line 1, column 0.

    如果出现了下列错误,那是因为用错了函数.把queryParser.Query改称queryParser.parse就通过了 org.apache.lucene.queryParser.ParseExc ...

  4. 学习笔记——模板模式Template

    模板模式,主要是利用多态来实现具体算法和父类逻辑的松耦合.父类中TemplateMethod内部定义了相应的算法操作顺序,子类负责实现相应的具体实现. 举例: 项目中曾遇到过一个需求,叫做高级价格体系 ...

  5. 学习笔记——工厂模式Factory

    Product是我们所需要获得的对象,一般的使用中,我们直接通过new获得新对象. 当需要大量获得对象时,每一次都new会很麻烦. <真菌世界>游戏中,一个星球上有很多树,一棵树会不断生成 ...

  6. 为什么做Web开发要选择PHP

    大部分互联网公司做WEb开发都选择PHP,PHP的优势在哪?你应该知道的 以前偶尔被人问到,为什么你(和大部分互联网公司)做Web开发要选择PHP, PHP有什么好处.简单的回答便是“PHP简单,开发 ...

  7. Android, BaseAdapter 处理大数据量时的优化

    Android优化 最常见的就是ListView, Gallery, GridView, ViewPager 的大数据优化  图片优化  访问网络的优化优化的原则: 数据延迟加载  分批加载  本地缓 ...

  8. java集合框架list和set

    为什么出现集合类? 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一中方式 数组和集合类同是容器,有何不同? 数组虽然也可以存储对象 ...

  9. Qt5:窗口各类位置

    在Qt程序中获取窗口位置的函数有  geometry()   ,   frameGeometry()  , pos() ,x() , y()等 下面来看看这些函数的区别 还有另外两个函数 size() ...

  10. Java枚举的3类语法

    一.最基本的用法 public enum TestEnum { IMG,TEXT,WORD } 使用时: TestEnum testEnum=TestEnum.WORD; System.out.pri ...