在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. ShellExecute快捷键大全

    文件夹,文件,网址可以创建快捷方式,控制面板 中的设置也可以创建快捷方式,下面是快捷方式的命令,使用方法:在桌面或文件夹的空白处点右键,选择新建,快捷方式,在"请键入项目的位置"输 ...

  2. linux视频学习7(ssh, linux启动过程分析,加解压缩,java网络编程)

    回顾数据库mysql的备份和恢复: show databases; user spdb1; show tables; 在mysql/bin目录下 执行备份: ./mysqldump -u root - ...

  3. Trie/Xor

    题目链接 /*有一个数组a1,a2,a3--an.找到一个连续子段[l,r],使得al ^ al+1 ^--^ ar达到最大. 一般思路:维护前缀异或+暴力: for(int i=1;i<=n; ...

  4. Jenkins中集成python,支持参数生成Makefile文件

    #!/usr/bin/env python import os print os.getenv("BUILD_NUMBER") print os.getenv("uuid ...

  5. elasticsearch 手动控制分片分布

    elasticsearch可以通过reroute api来手动进行索引分片的分配.  不过要想完全手动,必须先把cluster.routing.allocation.disable_allocatio ...

  6. 转:Selenium中的几种等待方式,需特别注意implicitlyWait的用法

    最近在项目过程中使用selenium 判断元素是否存在的时候 遇到一个很坑爹的问题, 用以下方法执行的时候每次都会等待很长一段时间,原因是因为对selenium实现方法了解不足导致一直找不到解决方法. ...

  7. 上传Android或Java库到Maven central repository(转载)

    主要介绍利用Sonatype将jar或aar提交到Maven的中央仓库. 是不是希望将自己的jar或是aar传到maven官方库中,在The Central Repository中可以被其他人搜索使用 ...

  8. php csv操作

    csv的写入数据: $data = array( array('qq号','登录时间','名称'), array('123456','2012-08-21 15:21:10'.chr(1),'我是来测 ...

  9. AS-->如何更高效的使用 Gradle, 快速build apk

    版权声明:欢迎转载,转载请注明出处;http://blog.csdn.net/angcyo 看本文之前,推荐先看我之前写的一篇文章: 传送门 日前Android Stuido 已经更新到 2.0.0 ...

  10. 利用id来进行树状数组,而不是离散化以后的val HDU 4417 离线+树状数组

    题目大意:给你一个长度为n的数组,问[L,R]之间<=val的个数 思路:就像标题说的那样就行了.树状数组不一定是离散化以后的区间,而可以是id //看看会不会爆int!数组会不会少了一维! / ...